我想允许匿名访问我的网站的根。如果我向site.com/home发出请求,它允许匿名访问。但是,如果我请求site.com/,我会看到一个登录页面。到目前为止,我已经做了以下内容:允许匿名访问MVC4操作
在web.config我授权为所有用户 “家”:
<location path="Home">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
在FilterConfig.cs添加以下AuthorizeAttribute:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
}
My Home Index控制器动作如下所示:
[AllowAnonymous]
public ActionResult Index()
{
return View();
}
我的路线如下所示:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Zoom",
url: "zoom/{id}",
defaults: new { controller = "Zoom", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这是否完成了路线?我完全错过了什么吗?
尝试了您的建议,但它没有工作,除非我实施它不正确。如果它在我请求时发挥作用/ Home不是让根请求发送到适当的控制器/操作的问题?当我去/家时,它显示访问是正确的,因为它提供了正确的回报。 – Alex
您是否在OnAuthorization方法中设置了休息以确保其正常工作?我知道这种方法可行,因为我在我的一个生产应用程序中使用它。 – dcp