且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义参数名称处理ASP.NET Web API的特殊字符MVC 4

更新时间:2023-09-01 13:51:52

使用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)