How to get base URL in Web API controller?
In the action method of the request to the url “http://localhost:85458/api/ctrl/” var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) ; this will get you http://localhost:85458
In the action method of the request to the url “http://localhost:85458/api/ctrl/” var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) ; this will get you http://localhost:85458
I’ve created a pared-down demo project for you. Source: https://github.com/bigfont/webapi-cors Api Link: https://cors-webapi.azurewebsites.net/api/values You can try the above API Link from your local Fiddler to see the headers. Here is an explanation. Global.ascx All this does is call the WebApiConfig. It’s nothing but code organization. public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { … Read more
Every class in the swagger JSON must have a unique schemaId. Swashbuckler tries to just use the class name as a simple schemaId, however if you have two classes in different namespaces with the same name (as you do) this will not work. As the error suggests, you can use the config setting “UseFullTypeNameInSchemaIds*” for … Read more
Putting it all together you get… protected void Application_Start() { HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false; }
Suffix the URL with a slash e.g. http://somedomain.com/api/people/staff.33311/ instead of http://somedomain.com/api/people/staff.33311.
You need the Microsoft.AspNet.WebApi.Core package. You can see it in the .csproj file: <Reference Include=”System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL”> <SpecificVersion>False</SpecificVersion> <HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.0.0\lib\net45\System.Web.Http.dll</HintPath> </Reference>
There is a brilliant blog post from Taiseer Joudeh with a detailed step-by-step description. Part 1: Token Based Authentication using ASP.NET Web API 2, Owin, and Identity Part 2: AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity Part 3: Enable OAuth Refresh Tokens in AngularJS App using ASP .NET Web API 2, … Read more
The issue stems from your Angular code: When withCredentials is set to true, it is trying to send credentials or cookies along with the request. As that means another origin is potentially trying to do authenticated requests, the wildcard (“*”) is not permitted as the “Access-Control-Allow-Origin” header. You would have to explicitly respond with the … Read more
Here Ok() is just a helper for the type OkResult which sets the response status to be HttpStatusCode.Ok…so you can just check if the instance of your action result is an OkResult…some examples(written in XUnit): // if your action returns: NotFound() IHttpActionResult actionResult = valuesController.Get(10); Assert.IsType<NotFoundResult>(actionResult); // if your action returns: Ok() actionResult = valuesController.Get(11); … Read more
Enable globally From the docs: httpConfiguration .EnableSwagger(c => { c.SingleApiVersion(“v1”, “A title for your API”); c.DescribeAllEnumsAsStrings(); // this will do the trick }); Enum/string conversion on particular property Also, if you want this behavior only on a particular type and property, use the StringEnumConverter: public class Letter { [Required] public string Content {get; set;} [Required] … Read more