LP1*_*P13 5 asp.net-mvc asp.net-mvc-routing
我有一个网址http://localhost/Home/DomSomething?t=123&s=TX ,我想将此网址路由到以下操作方法
public class HomeController
{
public ActionResult DoSomething(int taxYear,string state)
{
// do something here
}
}
Run Code Online (Sandbox Code Playgroud)
由于查询字符串名称与操作方法的参数名称不匹配,因此请求不会路由到操作方法.
如果我改变网址(仅用于测试)http://localhost/Home/DomSomething?taxYear=123&state=TX然后它的工作.(但我没有权限更改请求.)
我知道有一个Route属性,我可以申请的操作方法,并可以映射t到taxYear和s到state.
但是,我没有为此映射找到Route属性的正确语法,有人可以帮忙吗?
Win*_*Win 12
如果查询字符串参数始终为t和s,则可以使用前缀.请注意,它不再接受taxYear和state.
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
}
Run Code Online (Sandbox Code Playgroud)
如果要接受这两个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
}
Run Code Online (Sandbox Code Playgroud)
如果您不介意使用第三方软件包,可以使用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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5173 次 |
| 最近记录: |