MVC 路由参数
如果我有这条路线:
routes.MapRoute(
"BlogRoute", // Route name
"blog/{action}", // URL with parameters
new { controller = "Blog", action = "Index", id="abc" } // Parameter defaults
);
...并且在控制器中有这个 Index 方法:
public ActionResult Index(string id)
{
return View((object)id);
}
有人可以将该 id 参数从“abc”更改为其他参数吗?例如,通过将 ?id=somethingElse 附加到 URL?我尝试过,但并没有改变它。那么是否可以保证我在 Index 方法中总是得到“abc”?
基本上,当选择一个路由时,我需要发送一个硬编码字符串,并且我不希望用户能够通过 URL 或任何其他机制更改该字符串。就像“abc”是一个密码(它不是,但只是假设它是)。仅允许开发人员通过编辑 Global.asax.cs 来设置此字符串。
是否可以?
If I have this route:
routes.MapRoute(
"BlogRoute", // Route name
"blog/{action}", // URL with parameters
new { controller = "Blog", action = "Index", id="abc" } // Parameter defaults
);
... and have this Index method in the controller:
public ActionResult Index(string id)
{
return View((object)id);
}
Is it possible for someone to change that id parameter from "abc" to something else? For example, by appending ?id=somethingElse to the URL? I tried that but it didn't change it. So is it guaranteed that I'll always get "abc" in the Index method?
Basically I need to send a hardcoded string when one route is chosen and I don't want the user to be able to change this string via the URL or any other mechanism. It's like "abc" is a password (it's not but just assume it is). Only the developer is allowed to set this string by editing Global.asax.cs.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用正则表达式 /abc/ 为 id 参数添加约束
You can add a constraint for the id parameter using the regular expression /abc/