您可以覆蓋控制器
protected override void OnAuthorization(AuthorizationContext filterContext)
{
if ((string)(filterContext.RouteData.Values["action"]) == "Login")
{
filterContext.Cancel = true;
filterContext.Result = Login();
}
}
這工作,但它是一個黑客的OnAuthorization方法。用於測試
滿級代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApplication2.Controllers
{
[HandleError]
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
ViewData["Title"] = "About Page";
return View();
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
if ((string)(filterContext.RouteData.Values["action"]) == "Index")
{
filterContext.Cancel = true;
filterContext.Result = Index();
}
}
}
}
看起來你是正確的根據http://www.codeplex.com/aspnet/SourceControl/changeset/view/17272我猜這個屬性是在控制器級別應用的,並且從不給這個動作嘗試評估它。 – 2008-11-30 23:13:46
嗯,顯然你不能直接鏈接到codeplex上的源文件。 – 2008-11-30 23:16:10