asp.net-web-api

Model validation in Web API - Exception is thrown with out a throw statement?

南笙酒味 提交于 2021-01-27 07:38:19
问题 I have seen the model validation from here (Under section: Handling Validation Errors ). The code-snippet is as below in Web API public class ValidateModel : ActionFilterAttribute { public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, actionContext.ModelState); } base.OnActionExecuting(actionContext); }

Model validation in Web API - Exception is thrown with out a throw statement?

帅比萌擦擦* 提交于 2021-01-27 07:37:56
问题 I have seen the model validation from here (Under section: Handling Validation Errors ). The code-snippet is as below in Web API public class ValidateModel : ActionFilterAttribute { public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, actionContext.ModelState); } base.OnActionExecuting(actionContext); }

Web API not able to bind model for POST with utf-16 encoded XML

走远了吗. 提交于 2021-01-27 07:09:17
问题 I have a simple Web API controller with a POST method, that accepts an object. When the clients posts data as JSON the API works fine. Even when data is sent as XML with encoding="utf-8" , the model binds seamlessly (I have added the following line in WebApiConfig to use Xml Serialization instead of DataContract ) config.Formatters.XmlFormatter.UseXmlSerializer = true; Below is my ApiController: public class InfoController : ApiController { public HttpResponseMessage Post(InfoRequest

Tracking ASP.NET Web API with google analytics

匆匆过客 提交于 2021-01-27 06:27:12
问题 I'm developing an API for my website that users can access and use own their own websites/apps. I would like to keep usage statistics for the use of the different API calls I allow, and would like to use Google Analytics like I do for my website. Is it possible to track with Google Analytics on server side code? or specifically web api? Thanks 回答1: Google analytics have give the mobile code to track the statistics from server side. You may use that code to archive your goal. What this

Avoid bind any number to bool property

試著忘記壹切 提交于 2021-01-27 06:03:40
问题 I have simple ASP.NET Core WebApi with model public class Model { public bool? Value {get; set;} } and endpoint [HttpPost] public async Task<IActionResult> Create([FromBody] Model model) When I make a POST request with body { "Value" : 7676 } or { "Value" : 2955454545645645645645645645654534534540 } then model.Value == true How to avoid this? I need some error in this case, because 7676 is not the Boolean value. I found this question and this, but solution is not fit for me, because I have a

Web API Model properties are null

不想你离开。 提交于 2021-01-27 05:35:58
问题 My Controller is able to create the model object but all the properties related to model and assigned to null values Environment : VS 2010, ASP.NET MVC RC latest, jQuery 1.7.1 Following is the Web API Controller code public class Customer { public string Name { get; set; } public string City { get; set; } } public class UserController : ApiController { public Customer Post(Customer user) { return user; } } Following is the ajax calling code $.ajax('/api/user', { ContentType: "application/x

Api Explorer for external project or assembly

二次信任 提交于 2021-01-27 05:35:23
问题 I want to use Web API's Api Explorer in order to start generating my own documentation html pages. What I'm trying to aim at is something like described here: http://blogs.msdn.com/b/yaohuang1/archive/2013/01/20/design-time-generation-of-help-page-or-proxy-for-asp-net-web-api.aspx However, this project is a bit outdated and does not work with the current web api. I want to have: A console program that has api explorer core libraries installed in it. It takes in an assembly from another

Odata serialization error for SingleResult.Create on an empty IQueryable

我的梦境 提交于 2021-01-27 05:24:36
问题 I'm using OData v4 and trying to get a really simple controller working: Controller: public class ProductController : ODataController { readonly MasterDataEntities db = new MasterDataEntities(); [EnableQuery(PageSize = MaxPageSize)] public IQueryable<Product> Get() { return db.Products; } [EnableQuery] public SingleResult<Product> Get([FromODataUri] string key) { return SingleResult.Create(db.Products.Where(p => p.Code == key)); } } WebApiConfig: ODataModelBuilder builder = new

Odata serialization error for SingleResult.Create on an empty IQueryable

不问归期 提交于 2021-01-27 05:24:09
问题 I'm using OData v4 and trying to get a really simple controller working: Controller: public class ProductController : ODataController { readonly MasterDataEntities db = new MasterDataEntities(); [EnableQuery(PageSize = MaxPageSize)] public IQueryable<Product> Get() { return db.Products; } [EnableQuery] public SingleResult<Product> Get([FromODataUri] string key) { return SingleResult.Create(db.Products.Where(p => p.Code == key)); } } WebApiConfig: ODataModelBuilder builder = new

how to dynamic jsonignore according to user authorize?

女生的网名这么多〃 提交于 2021-01-27 04:21:10
问题 I use Metadata and JsonIgnore to remove special field from being serializing. [Authorize(Roles = "admin")] public class UserController : ApiController { public IEnumerable<user> Get() { using (var mydb = new ModelContainer()) { return mydb.userSet.ToList(); } } } [MetadataType(typeof(user_Metadata))] public partial class user { private class user_Metadata { [JsonIgnore] public virtual password { get; set; } public virtual adminFile { get; set; } } } How can I dynamic control which field