.net - 如何检查“非前缀”
问题描述
这是我的RegisterRoutes
内心RouteConfig.cs
:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Worktypes",
url: "Clinic/Worktypes/{action}/{id}",
defaults: new { controller = "Worktypes", action = "Index", id = UrlParameter.Optional }
);
// default (finance)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
哪个有效,访问/Clinic/Worktypes
. 问题是,如果我/Worktypes
在 url 中输入数字,它也可以工作。如何防止访问“非前缀”控制器?
解决方案
我不认为这是最优雅的解决方案,也许有更好的解决方案。
但是,我在我的一个项目中遇到了类似的问题,我通过使用命名空间约束来解决。
因此,在您的路由注册中,前提是您的前缀控制器具有与需要转到默认路由的其余控制器不同的命名空间
var route = routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// lets assume that your non prefixed controllers are under the WebApp.Controllers
// namespace
namespaces: new []{ "WebApp.Controllers" }
);
// note, this is an important part. Basically you tell this route to match only the
// controller that are under that namespace, specified in the route
route.DataTokens["UseNamespaceFallback"] = false;
希望这可以帮助
推荐阅读
- c++ - 删除两个向量C++中的相似元素
- java - 如何在 Java 上实现多线程就地合并排序
- java - 为什么这个java方法不起作用?它应该消除字符串中的空格
- notepad++ - Notepad ++如何在小数点后仅添加两位数字
- python - 从文本文件中删除重复部分而不删除第一次出现
- excel - “堆叠”高级过滤器 Excel
- python - AttributeError: 'float' 对象在使用 seaborn 时没有属性 'shape'
- asset-pipeline - Rails 5.1 将 js 文件从 app/assets/javascripts 移动到 app/javascripts
- sql - oracle 子查询与 order by 不工作
- angular - 遍历firebase列表中的列表