asp.net-web-api

Adding a custom query backed Navigation Property to ODataConventionModelBuilder

人走茶凉 提交于 2019-12-28 06:45:16
问题 Situation I created the following Model classes public class Car { public int Id {get;set;} public string Name {get;set;} public virtual ICollection<PartState> PartStates {get;set; } } public class PartState { public int Id {get;set;} public string State {get;set;} public int CarId {get;set;} public virtual Car Car {get;set;} public int PartId {get;set;} public virtual Part Part {get;set;} } public class Part { public int Id {get;set;} public string Name {get;set;} } And a matching DbContext

Adding a custom query backed Navigation Property to ODataConventionModelBuilder

↘锁芯ラ 提交于 2019-12-28 06:45:14
问题 Situation I created the following Model classes public class Car { public int Id {get;set;} public string Name {get;set;} public virtual ICollection<PartState> PartStates {get;set; } } public class PartState { public int Id {get;set;} public string State {get;set;} public int CarId {get;set;} public virtual Car Car {get;set;} public int PartId {get;set;} public virtual Part Part {get;set;} } public class Part { public int Id {get;set;} public string Name {get;set;} } And a matching DbContext

Why is the Web Api returning empty json results with “$ref=”?

风格不统一 提交于 2019-12-28 06:35:44
问题 I disabled lazy loading and proxy creation in EF5 before returning any results in the Web Api. Everything worked fine. However, when i start to Include() other entities for eager fetching, some of the JSON objects look like this: { $ref: "14" }, .. correct objects .. { $ref: "6" }, .. What is this "$ref" that gets included in the list of results? 回答1: It's coming from the JSON.NET serializer. It's preserving object references. Take a look at the following blog post. I guess you have some

Route parameter with slash “/” in URL

时光毁灭记忆、已成空白 提交于 2019-12-28 05:56:09
问题 I know you can apply a wildcard in the route attribute to allow / such as date input for example: [Route("orders/{*orderdate}")] The problem with wildcard is only applicable to the last paramter in URI. How do I solve the issue if want to have the following URI: [Route("orders/{orderdate}/customers")] Update: I know there are few options to solve the issue by refactoring the code so please do not offer a solution something like: change the route template to [Route("orders/customers/{orderdate

How to override all standard error pages in WebAPI

别来无恙 提交于 2019-12-28 05:52:43
问题 My beautiful REST webservice works great. Except if I visit pages like ~/ , which returns the default IIS 403 Forbidden page (even using Fiddler and specifying only Accept: application/json ). I want nothing but JSON or XML errors. Is there a way to override ALL exceptions with a custom exception handler? or a default controller to handle all unknown requests? What's the simplest, and the most correct (if different), way to handle this so that clients need only parse REST API-friendly XML

Adding multiple Set-Cookie Headers in ASP.NET Web

[亡魂溺海] 提交于 2019-12-28 05:36:32
问题 I faced a problem. When you add multiple Set-Cookie headers to the response headers.Add("Set-Cookie", "a=b;Path=/;"); headers.Add("Set-Cookie", "c=d;Path=/;"); actually they are combined and only one header is sent with comma-separated cookies Set-Cookie: a=b;Path=/;,c=d;Path=/; According to RFC2109 it is a valid syntax. But it is not according to RFC6265, which deprecates RFC2109 Moreover latest browsers does not support this comma-separated syntax as well. Tested on IE9 , Firefox 13 and

Conditional member serialization based on query parameter?

浪子不回头ぞ 提交于 2019-12-28 04:33:12
问题 I would like to control which properties from my model are serialized to my WebAPI2 JSON response, based on matching a query parameter to an attribute. I mainly want to do this to reduce bandwidth on GETs without causing a proliferation of ViewModel classes. For example: GET /books/1?format=summary public class Book { [SerializeFormat("summary")] public int Id { get; set; } [SerializeFormat("summary")] public string Title { get; set; } public string Contents { get; set; } } or

Web API: Configure JSON serializer settings on action or controller level

ぃ、小莉子 提交于 2019-12-28 03:45:09
问题 Overriding the default JSON serializer settings for web API on application level has been covered in a lot of SO threads. But how can I configure its settings on action level? For example, I might want to serialize using camelcase properties in one of my actions, but not in the others. 回答1: Option 1 (quickest) At action level you may always use a custom JsonSerializerSettings instance while using Json method: public class MyController : ApiController { public IHttpActionResult Get() { var

ASP.NEt MVC using Web API to return a Razor view

时光怂恿深爱的人放手 提交于 2019-12-28 03:38:07
问题 How to make the View returned by the controller and generated by Razor get the data from the api i want to keep the razor engine view and use the api the original mvc controller returns the view with the data as parameter now i want the data from the api MVC controller public class ProductController : Controller { public ActionResult Index() { return View(); } Api Controller public class ProductsController : ApiController { private ApplicationDbContext db = new ApplicationDbContext(); // GET

C# unsupported grant type when calling web api

a 夏天 提交于 2019-12-28 03:32:06
问题 I am trying to perform a Post to my WebAPI from a c# WPF desktop app. No matter what I do, I get {"error":"unsupported_grant_type"} This is what I've tried (and I've tried everything I could find): Also dev web api currently active for testing: http://studiodev.biz/ base http client object: var client = new HttpClient() client.BaseAddress = new Uri("http://studiodev.biz/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue