开发者

passing a query string variable that is a keyword in c#

I am working with an API that is posting to a URL. One of the variables is is passing is called 'event'. Since event is a keyword in C# my code is getting hung up on it.

public ActionResult Index(string event,string email, string category, string reason, string response, string type, string status)
        {
 开发者_高级运维           return View();
        }

What is the workaround for this?


Keywords can usually not be used for argument names. However you can change your definition into:

public ActionResult Index(string @event, string email, ...) {


From §2.4.2 Identifiers of the C#4 spec:

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

This doesn't apply just to other languages, but also when using Reflection from C#, which is (I assume) what MVC does. So your method should be:

public ActionResult Index(string @event, string email, string category, string reason,
                          string response, string type, string status)
{
    return View();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜