简体   繁体   English

带有格式的.NET Core MVC属性路由

[英].NET Core MVC Attribute Routing with Format

A client wants to be able to put a file extension on the end of our WebApi GET calls which will determine the output format. 客户希望能够在我们的WebApi GET调用的末尾放置文件扩展名,这将确定输出格式。

For example, USE: 例如,USE:

/api/v1.0/customers.xml

or 要么

/api/v1.0/customers.json

or this URL with a header attribute of 'accept: application/xml' or 'accept: application/json' 或带有标头属性“ accept:application / xml”或“ accept:application / json”的URL

/api/v1.0/customers

I have the correct format working with the passing the header attribute. 我具有传递标头属性的正确格式。 I can also get the correct format to work with a file extension within the URL so long as it isn't the "default" route for the controller. 只要它不是控制器的“默认”路由,我也可以获得与URL中的文件扩展名一起使用的正确格式。

So adding attribute routing defined below as: 因此,添加以下定义的属性路由:

[HttpGet( ".{format?}" )]
public async Task<IActionResult> Get()
{ ... }

[HttpGet( "{id:long}.{format?}" )]
public async Task<IActionResult> Get( long id )
{ ... }

The first method (without any input parameters) will throw an error. 第一种方法(没有任何输入参数)将引发错误。 The second method will work. 第二种方法将起作用。

I can get around this by adding another HttpGet attribute to the "default" method like so: 我可以通过在“默认”方法中添加另一个HttpGet属性来解决此问题,如下所示:

[HttpGet]
[HttpGet( ".{format}" )]
public async Task<IActionResult> Get()
{ ... }

I've created a derived class from HttpGet. 我已经从HttpGet创建了派生类。 What I'd like to do is have this derived class add two routes to the routing table per the one attribute, one with the format and one without. 我想做的是让这个派生类为每个属性向路由表添加两条路由,一条具有格式,另一条没有格式。 I haven't figured out how to do this. 我还没有弄清楚该怎么做。

So I want it derived HttpGet to act normally and put a route in the routing table per its normal functionality but I'd also like it to add an additional route with the ".{format}" added to the end or the route. 因此,我希望它派生的HttpGet可以正常运行,并按照其正常功能将路由放入路由表中,但我也希望它添加一条附加路由,并在末尾或路由中添加“。{format}”。 This should handle all of my situations. 这应该处理我所有的情况。

I want the two methods to look like what is displayed below and the extra "format" route added automatically (because of the new functionality added with the derived HttpGet attribute): 我希望这两种方法看起来像下面显示的一样,并且自动添加了额外的“格式”路由(由于使用派生的HttpGet属性添加了新功能):

[HttpGet()]
public async Task<IActionResult> Get()
{ ... }

[HttpGet( "{id:long}" )]
public async Task<IActionResult> Get( long id )
{ ... }

Anyone have any ideas? 有人有想法么? Should I maybe try a different approach? 我应该尝试其他方法吗?

For clarity, I need an additional route for every existing route with ".{format}" added on the end because adding the optional "?" 为了清楚起见,我需要为每个现有路由附加一条路由,并在末尾添加“。{format}”,因为添加了可选的“?” character does not work if there is not action within the route. 如果路线内没有任何动作,则角色不起作用。

I created a derived class from the HttpGetAttribute that appends ".{format}" in the correct location of the routing string. 我从HttpGetAttribute创建了派生类,该派生类在路由字符串的正确位置附加了“。{format}”。

So the controller actions look like this: 因此,控制器动作如下所示:

[HttpGet]
[HttpGetWithFormat]
public async Task<IActionResult> Get()
{ ... }

[HttpGet( "{id:long}" )]
[HttpGetWithFormat( "{id:long}" )]
public async Task<IActionResult> Get( long id )
{ ... }

This works but again it's not my ideal solution. 这有效,但再次不是我理想的解决方案。 As I have stated in my question, I should be able to create one custom attribute that does both things (HttpGet and HttpGetWithFormat). 正如我在问题中所说的,我应该能够创建一个可以同时完成这两项工作的自定义属性(HttpGet和HttpGetWithFormat)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM