我想允許匿名訪問我的網站的根。如果我向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