How to match query parameters with action parameters in MVC?

I have a url http://localhost/Home/DomSomething?t=123&s=TXand I want to redirect this url to the following action method

public class HomeController
{
   public ActionResult DoSomething(int taxYear,string state)
   {
      // do something here
   }
}

Because the query string names do not match the parameter name of the action method, the query does not route the action method.

If I changed the URL (for testing only) to http://localhost/Home/DomSomething?taxYear=123&state=TX, then its working. (But I do not have access to modify the request.)

I know that an attribute Routecan be applied to an action method and can display tin taxYearand son state.

However, I do not find the correct Route attribute syntax for this mapping, can anyone help?

+4
source share
1

1

t s, . , taxYear .

http://localhost:10096/home/DoSomething?t=123&s=TX

public ActionResult DoSomething([Bind(Prefix = "t")] int taxYear, 
   [Bind(Prefix = "s")] string state)
{
    // do something here
}

2

URL-, , -

http://localhost:10096/home/DoSomething?t=123&s=TX
http://localhost:10096/home/DoSomething?taxYear=123&state=TX

public ActionResult DoSomething(
    int? t = null, int? taxYear = null, string s = "",  string state = "")
{
    // do something here
}

3

, ActionParameterAlias ​​. URL.

http://localhost:10096/home/DoSomething?t=123&s=TX
http://localhost:10096/home/DoSomething?taxYear=123&state=TX

[ParameterAlias("taxYear", "t")]
[ParameterAlias("state", "s")]
public ActionResult DoSomething(int taxYear, string state)
{
    // do something here
}
+9

Source: https://habr.com/ru/post/1668460/


All Articles