Single controller with multiple GET methods in ASP.NET Web API

In Web API I had a class of similar structure: public class SomeController : ApiController { [WebGet(UriTemplate = "{itemSource}/Items")] public SomeValue GetItems(CustomParam parameter) { ... } [WebGet(UriTemplate = "{itemSource}/Items/{parent}")] public SomeValue GetChildItems(CustomParam parameter, SomeObject parent) { ... } } Since we could map individual methods, it was ve

ASP.NET Web API中具有多个GET方法的单个控制器

在Web API中,我有一类相似的结构: public class SomeController : ApiController { [WebGet(UriTemplate = "{itemSource}/Items")] public SomeValue GetItems(CustomParam parameter) { ... } [WebGet(UriTemplate = "{itemSource}/Items/{parent}")] public SomeValue GetChildItems(CustomParam parameter, SomeObject parent) { ... } } 由于我们可以映射单个方法,因此在正确的位置获得正确的请求非常

How to change default Web API 2 to JSON formatter?

I have a Web API project that returns some product data. It negotiates the return type correctly depending on the Accept header (JSON/XML) of the request. The problem is, if no Accept header is specified it returns XML, but I want it to return JSON by default http://website.com/MyPage?type=json // returns json http://website.com/MyPage?type=xml // returns xml http://website.com/MyPage // retur

如何将默认的Web API 2更改为JSON格式化程序?

我有一个返回一些产品数据的Web API项目。 它根据请求的Accept头(JSON / XML)正确地协商返回类型。 问题是,如果没有指定Accept头,它将返回XML,但我希望它默认返回JSON http://website.com/MyPage?type=json // returns json http://website.com/MyPage?type=xml // returns xml http://website.com/MyPage // returns xml by default 这是我目前的代码看起来像: GlobalConfiguration.Configuration.Formatters.XmlForm

How to change default ASP.NET MVC Web API media formatter?

I have a Web API project that returns some product data. It negotiates the return type correctly depending on the Accept header (JSON/XML) of the request. The problem is, if no Accept header is specified it returns JSON, but I want it to return XML by default. How do I change the content negotiation defaults in Global.asax? Found a solution via http://www.strathweb.com/2013/06/supporting-onl

如何更改默认的ASP.NET MVC Web API媒体格式化程序?

我有一个返回一些产品数据的Web API项目。 它根据请求的Accept头(JSON / XML)正确地协商返回类型。 问题是,如果没有指定Accept头,它将返回JSON,但我希望它默认返回XML。 如何更改Global.asax中的内容协商默认值? 通过http://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/找到解决方案 在WebApiConfig.Register()添加了这个: config.Formatters.Clear(); config.Formatters.A

Returning http status code from Web Api controller

I'm trying to return a status code of 304 not modified for a GET method in a web api controller. The only way I succeeded was something like this: public class TryController : ApiController { public User GetUser(int userId, DateTime lastModifiedAtClient) { var user = new DataEntities().Users.First(p => p.Id == userId); if (user.LastModified <= lastModifiedAtCl

从Web Api控制器返回http状态码

我试图返回一个状态代码304没有修改的GET方法在一个web api控制器。 我成功的唯一方式就是这样的: public class TryController : ApiController { public User GetUser(int userId, DateTime lastModifiedAtClient) { var user = new DataEntities().Users.First(p => p.Id == userId); if (user.LastModified <= lastModifiedAtClient) { throw new HttpResponseExcepti

What problems could arise if I use POST instead of PUT and DELETE?

I'm just starting to use Web API and though I found it really easy to create the methods and some configurations I needed, now I run into a problem I don't know how to solve. Some of the applications that will consume my services are very old and don't support DELETE and PUT methods (j2me applications for example) I have found that it is possible to do some kind of method emulatio

如果我使用POST而不是PUT和DELETE,会出现什么问题?

我刚刚开始使用Web API,尽管我发现创建方法和我需要的一些配置非常简单,现在我遇到了一个我不知道如何解决的问题。 一些将使用我的服务的应用程序非常陈旧,不支持DELETE和PUT方法(例如,j2me应用程序) 我发现可以通过传递这样的东西来进行某种方法模拟: _method=DELETE|PUT 但是,我并不确定Web API是否能够解释这一点,而且,我也没有最明确的想法如何去做。 由于这些原因,我只是在使用POST方法来处理更新和删除

Custom method names in ASP.NET Web API

I'm converting from the WCF Web API to the new ASP.NET MVC 4 Web API. I have a UsersController, and I want to have a method named Authenticate. I see examples of how to do GetAll, GetOne, Post, and Delete, however what if I want to add extra methods into these services? For instance, my UsersService should have a method called Authenticate where they pass in a username and password, howeve

ASP.NET Web API中的自定义方法名称

我正在从WCF Web API转换到新的ASP.NET MVC 4 Web API。 我有一个UsersController,我想要一个名为Authenticate的方法。 我看到了如何执行GetAll,GetOne,Post和Delete的例子,但是如果我想向这些服务中添加额外的方法呢? 例如,我的UsersService应该有一个名为Authenticate的方法,它们传递用户名和密码,但它不起作用。 public class UsersController : BaseApiController { public string GetAll() { re

mvc 5 check user role

How in mvc 5 I can found out role of logged user? I made the user by this code private bool AddUserAndRole() { IdentityResult ir; var rm = new RoleManager<IdentityRole> (new RoleStore<IdentityRole>(new ApplicationDbContext())); ir = rm.Create(new IdentityRole("admin")); var user = new ApplicationUser() { UserName = "Admin" };

mvc 5检查用户角色

如何在MVC 5我可以找到登录用户的角色? 我通过此代码创建了用户 private bool AddUserAndRole() { IdentityResult ir; var rm = new RoleManager<IdentityRole> (new RoleStore<IdentityRole>(new ApplicationDbContext())); ir = rm.Create(new IdentityRole("admin")); var user = new ApplicationUser() { UserName = "Admin" }; var result =

ASP.NET Core Web API exception handling

I started using ASP.NET Core for my new REST API project after using regular ASP.NET Web API for many years. I don't see a good way to handle exceptions in ASP.NET Core Web API. I tried to implement exception handling filter/attribute: public class ErrorHandlingFilter : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) { HandleExceptionAs

ASP.NET Core Web API异常处理

在使用常规ASP.NET Web API多年后,我开始为新的REST API项目使用ASP.NET Core。 我没有看到在ASP.NET Core Web API中处理异常的好方法。 我试图实现异常处理过滤器/属性: public class ErrorHandlingFilter : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) { HandleExceptionAsync(context); context.ExceptionHandled = true; } private stati

Difference between MVC 5 Project and Web Api Project

I am new to ASP.NET MVC and Web API and trying to get the basics. AFAIK, we have project templates in VS 2013, named as MVC , Web API and Both of them together . I have gone through the tutorials and learned that we can make an API by using MVC alone as well as with Web API Template. So, What are the differences between these, based on Architecture and Usage ? Basically, a Web API controll

MVC 5项目和Web Api项目的区别

我是ASP.NET MVC和Web API的新手,并试图获得基础知识。 AFAIK,我们在VS 2013中有项目模板,命名为MVC , Web API和Both of them together 。 我已经阅读了教程,并了解到我们可以通过单独使用MVC以及使用Web API模板来制作API。 那么,基于体系结构和用法的这些差异有哪些? 基本上, Web API控制器是一个MVC控制器,它使用HttpMessageResponse作为其响应的基本类型,而不是ActionResponse 。 他们在大多数其他方面都

Recommended ServiceStack API Structure

I'm trying work out the best way to structure our API; we have Reviews which we've setup in a standard REST structure (list one, list all, create, update etc). Where it doesn't quite fit the examples is: each review can be linked to one or more other types eg Event, Location or Thing. My thinking is the urls would be along the lines of: /event/reviews/ (or the reverse of this eg /

推荐的ServiceStack API结构

我正在尝试制定构建API的最佳方式; 我们有评论,我们已经在标准的REST结构(列表一,列出所有,创建,更新等)中设置。 不太适合的例子是:每个评论可以链接到一个或多个其他类型,例如事件,位置或事物。 我的想法是这些网址应该是:/ event / reviews /(或者这个eg / reviews / event /)的相反位置/ location / reviews / / thing / reviews / 然而,我可以看到的问题是每个这些的“GET”应该返回父对象,即事件。 所