如何让 ASP.NET MVC 设置区域的默认控制器和操作?
我创建了一个包含区域的新 ASP.NET MVC 项目,并且尝试将控制器操作设置为用户访问该区域时的默认控制器操作。
我添加了一个名为“Login”的区域,现在我有 Areas/Login/
并添加了 LoginController
。
我试图将此控制器设置为在用户导航到网站时调用。 如果我在浏览器中输入 www.test.com/Login/Login
我可以访问它,但我不知道如何在 global.asax
中设置路由以指向此控制器作为默认值。
如何在 ASP.NET MVC 中做到这一点?
I've created a new ASP.NET MVC project with areas and I'm trying to set a controller action to be the default controller action if the user visits that area.
I added an area called 'Login' now I have Areas/Login/
and I added LoginController
.
I am trying to set this controller to be invoked when the user navigates to the website.
I can access it if I type in browser www.test.com/Login/Login
but I don't know how to set routing in global.asax
to point to this controller as the default.
How do I do that in ASP.NET MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您创建区域时,MVC 是否没有在 Areas/[AreaName] 文件夹下创建
[AreaName]AreaRegistration
类?在那里您会发现与此类似的区域注册。将 defaults 参数的controller =
部分修改为您要默认使用的控制器名称(登录):When you created your area, did MVC not create the
[AreaName]AreaRegistration
class under the Areas/[AreaName] folder? In there you will find the area registration that looks similar to this. Modify thecontroller =
portion of the defaults parameter to the controller name (Login) you want to use by default:如果您使用
[Authorize]
属性装饰您的 HomeController(或者实际上是所有需要用户登录的控制器),则 ASP.NET MVC 会自动将用户重定向到登录屏幕(如果他们是这样的话)未登录。用法示例:
另外,您可能想阅读 开放重定向攻击漏洞(和修复)。
If you decorate your HomeController (or really, all of your controllers that require someone to be logged in) with the
[Authorize]
attribute, ASP.NET MVC will automatically redirect people to the login screen if they are not logged in.Example usage:
Also, you may want to read up on the Open Redirect attack vulnerability (and fix).