【发布时间】:2016-12-02 10:01:14
【问题描述】:
我想在执行控制器操作之前验证 url。 用户使用如下所示的 url 运行应用程序:
https://appAddress/appAdress2/{first}/?secondId=ASD&thirdId=FGH&fourthId=123
"first" and "secondId" values are mandatory and they are string.
"thirdId" and "fourthId" are optional and "thirdId" is string and "fourthId" is int.
我可以这样做吗,例如使用 attibute 还是应该检查控制器操作中的值?
public ActionResult MyAction()
{
//todo: validation
}
我使用 ASP.NET MVC 5
【问题讨论】:
-
你处理的是哪个 MVC 版本? MVC 5 具有属性路由约束规则,可以根据需要自定义任何 URL,即
{action}/?id2=val2&id3=val3&id4=val4。确保您的操作方法也包含 4 个参数。 -
@TetsuyaYamamoto - 实际上,RouteAttribute 不适用于查询字符串值,所以这是不可能的。
-
不清楚你在问什么。您想以什么方式“验证”参数?如果他们没有通过验证会发生什么? 404 没找到?还有什么?
-
@NightOwl888 如果错误,系统重定向到错误页面。
-
@TetsuyaYamamoto 我使用 MVC 5
标签: .net asp.net-mvc validation routes asp.net-mvc-routing