asp.net-web-api2

How to get base URL in Web API controller?

为君一笑 提交于 2019-11-28 06:08:04
I know that I can use Url.Link() to get URL of a specific route, but how can I get Web API base URL in Web API controller? Kiran Challa You could use VirtualPathRoot property from HttpRequestContext ( request.GetRequestContext().VirtualPathRoot ) 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 Url.Content("~/") worked for me! This is what I use: Uri baseUri = new Uri(Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.PathAndQuery, String.Empty

Returning generated CSS from an MVC5 or Web API 2 controller action

穿精又带淫゛_ 提交于 2019-11-28 06:02:56
问题 In our multi-tenant application we have a need to customize the styles used per-tenant. We currently plan to do so using LESS and variables in the following way on the client : Download dependent LESS files from server Call web service to get configuration object Form string of valid LESS with variables defined Use less.js compiler to compile LESS based on these variables and the fixed LESS files from step 1 This approach has a number of downsides: Clients can behave badly Some browsers have

Default model example in Swashbuckle (Swagger)

非 Y 不嫁゛ 提交于 2019-11-28 05:28:39
I'm running ASP WebAPI 2 and successfully installed Swashbuckle. I am trying to figure out how one defines what the default schema values are? For example, on the Swagger live demo site they changed the default value of pet to "doggie". They also defined the allowable values for status. ( Live Demo ) Well the code of vgaspar.trivix did not work completly for me, the default values did not get set for the schema. Also i got an NullPointerException . I managed to get it working as intended by editing the Apply method and manipulated the schemaRegistry like this: public void Apply(Operation

Issue with sending XML to Web Api via http Post request

♀尐吖头ヾ 提交于 2019-11-28 04:52:29
问题 I would like to send the following XML document to my Web Api 2 controller; however, the [FromBody] parameter is always null. Here is the request XML body: <razorInbound server="PortfolioExposureServer" request="lookupRiskPointRecord"> <caller>RazorClient</caller> <responseMode>xml</responseMode> <body> <conditions> <fromOffset>0</fromOffset> <top>100</top> <condition> <keyPath> <keyElement nodeOffset='1'>Currency</keyElement> <keyElement nodeOffset='2'>ID</keyElement> </keyPath> <lookupValue

Where can I find a NuGet package for upgrading to System.Web.Http v5.0.0.0?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 04:47:54
Just upgraded an ASP.NET MVC4 project to use Unity.WebApi version 5.0.0.0 and it requires System.Web.Http v 5.0.0.0 as per the following error: Assembly 'Unity.WebApi, Version=5.1.0.0, Culture=neutral, PublicKeyToken=43da31bc42a85347' uses 'System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' I am currently referencing System.Web.Http v4.0 but have the following NuGet packages upgraded to their respective latest versions:

MVC 5 Web API with Facebook access token to RegisterExternal without need of Cookie

北战南征 提交于 2019-11-28 04:25:36
Setup: New MVC5 Project with just Web API. Added Facebook AppId and Secret. I can get Token for my Web API from Token endpoint by passing in UserName and Password. Then use that token for further calls. BUT I want to register new users with the help of Facebook SDK in iOS app. I am using Facebook SDK to get Access Token. (Assume at this point, I have an Access Token). Next thing I know is to call api/Account/RegisterExternal endpoint by passing this token in Authorization header with Bearer [Access Token] but this result in 500 server error. I guess I know the reason, Cookie is missing. I made

How to configure Web Api 2 to look for Controllers in a separate project? (just like I used to do in Web Api)

↘锁芯ラ 提交于 2019-11-28 04:18:26
I used to place my controllers into a separate Class Library project in Mvc Web Api. I used to add the following line in my web api project's global.asax to look for controllers in the separate project: ControllerBuilder.Current.DefaultNamespaces.Add("MyClassLibraryProject.Controllers"); I never had to do any other configuration, except for adding the above line. This has always worked fine for me. However I am unable to use the above method to do the same in WebApi2. It just doesn't work. The WebApi2 project still tries to find the controllers in its own project's controllers folder. --

Global exception handling in OWIN middleware

泪湿孤枕 提交于 2019-11-28 03:49:47
I'm trying to create a unified error handling/reporting in ASP.NET Web API 2.1 Project built on top of OWIN middleware (IIS HOST using Owin.Host.SystemWeb). Currently I used a custom exception logger which inherits from System.Web.Http.ExceptionHandling.ExceptionLogger and uses NLog to log all exceptions as the code below: public class NLogExceptionLogger : ExceptionLogger { private static readonly Logger Nlog = LogManager.GetCurrentClassLogger(); public override void Log(ExceptionLoggerContext context) { //Log using NLog } } The I want to change the response body for all API exceptions to a

How do I unit test web api action method when it returns IHttpActionResult?

放肆的年华 提交于 2019-11-28 02:48:00
Let's assume this is my action method public IHttpActionResult Get(int id) { var status = GetSomething(id); if (status) { return Ok(); } else { return NotFound(); } } Test will be var httpActionResult = controller.Get(1); How do I check my http status code after this? 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

Data annotations in Swagger

試著忘記壹切 提交于 2019-11-28 02:34:37
问题 I am using ASP.NET and Swagger that exposes a complex type that accepts a POST. It has a number of string fields that have different restricted lengths. How can I reflect that in the Swagger UI? 回答1: You can annotate the properties with the StringLengthAttribute from System.ComponentModel.DataAnnotations . For instance: [StringLength(10)] public String Name {get;set;} will become: "name": { "minLength": 0, "maxLength": 10, "type": "string" } And this: [StringLength(10, MinimumLength = 5)]