自定义参数名称处理ASP.NET Web API的特殊字符MVC 4 [英] Custom Parameter Names With Special Characters in ASP.NET Web API MVC 4

查看:995
本文介绍了自定义参数名称处理ASP.NET Web API的特殊字符MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求建立一个获取方法,需要在以下URL的参数名:

毫秒级
MS-对比
MS-郎

正如你所看到的,所有的名字在他们一个破折号,这是不可能在C#。如何将我的方法映射到这些参数名称?

 公开的Htt presponseMessage获取(int标,串对比串郎)


解决方案

使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.fromuriattribute%28v=vs.108%29.aspx\">FromUriAttribute

 公开的Htt presponseMessage获取([FromUri(NAME =毫秒级)] int标,[FromUri(NAME =MS-对比)的字符串对比, [FromUri(NAME =MS-郎)的字符串郎)

I have a requirement to create a Get method which takes the following parameter names in the URL:

ms-scale ms-contrast ms-lang

As you can see, all the names have a dash in them which is not possible in C#. How can I map my method to these parameter names?

public HttpResponseMessage Get(int scale, string contrast string lang)

解决方案

Use FromUriAttribute

public HttpResponseMessage Get([FromUri(Name = "ms-scale")]int scale, [FromUri(Name = "ms-contrast")]string contrast, [FromUri(Name = "ms-lang")]string lang)

这篇关于自定义参数名称处理ASP.NET Web API的特殊字符MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆