ASP.NET MVC 操作和虚拟目录具有相同的路径
当控制器操作使用与虚拟目录相同的 URL 时,是否需要任何特殊路由或 IIS 配置?
我有一个 ASP.NET MVC 1.0 应用程序,需要将 Windows 身份验证应用于单个操作(“/Login/FromWindows”)。为此,我们设置了一个与操作具有相同路径的虚拟目录(例如“/Login/FromWindows”),并在 IIS 中启用了 Windows 身份验证。
当我访问 /Login/FromWindows URL 时,我得到一个空的 HTTP 200 响应,并且服务器文本日志中没有记录任何内容。 “FromWindows”操作应该记录消息并将用户重定向到主页。
看起来操作代码根本没有被执行,因此可能与虚拟目录发生冲突。
Global.asax.cs 中的路由配置
public static void RegisterRoutes(RouteCollection routes)
{
// snipped: ignored routes for images, scripts, etc.
routes.MapRoute( "Default", "{controller}/{action}",
new { controller = "Home", action = "Index" } );
}
Is any special routing or IIS config needed when a controller action uses the same URL as a virtual directory?
I have an ASP.NET MVC 1.0 application that needs Windows Authentication applied to a single action ("/Login/FromWindows"). To do this, we've setup a virtual directory with the same path as the action (e.g. "/Login/FromWindows") and enabled Windows Authentication on it in IIS.
When I visit the /Login/FromWindows URL, I get an empty HTTP 200 response and nothing is logged in the server text log. The "FromWindows" action should be logging messages and redirect the user to the home page.
It seems like the action code is simply not being executed, so there is possibly a conflict with the virtual directory.
Route config in Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
// snipped: ignored routes for images, scripts, etc.
routes.MapRoute( "Default", "{controller}/{action}",
new { controller = "Home", action = "Index" } );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的,操作代码没有被执行。这是因为现有文件路径(虚拟或非虚拟)优先于 MVC 路由规则。
为什么使用虚拟目录?只需在 web.config 中将身份验证设置为 windows 并在相应的操作方法上使用
[authorize]
属性即可。Web.config:
操作方法:
访问 http://www .asp.net/mvc/tutorials/authenticating-users-with-windows-authentication-vb 有关使用 Windows 身份验证的 mvc 的更多信息。
You are right, the action code isn't being executed. That's because existing file paths (virtual or not) take precedence over MVC routing rules.
Why are you using a virtual directory? Just set authentication to windows in the web.config and use the
[authorize]
attribute over the corresponding action methods.Web.config:
Action Method:
Visit http://www.asp.net/mvc/tutorials/authenticating-users-with-windows-authentication-vb for more information on mvc with windows authentication.
很简单,就是使用上面提到的 Chevex 的 [Authorize] 属性,或者如果您想要更多,您可以通过为您的业务扩展它来自定义授权。恕我直言。
Just simple is using [Authorize] attribute a Chevex mention above, or if you want more, you can customize the Authorize by extension it for your business. IMHO.