在 asp.net mvc 应用程序中,我试图重定向到 Application_AuthenticateRequest 中的新路由。如果用户未通过身份验证,那么我将重定向到不同的 url。以下是代码,
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
var loginUrl = urlHelper.Action("Login", "Default");
Response.Redirect(loginUrl);
}
在上面的代码中,即使在 Application_Start 事件发生路由注册后,urlHelper.Action 方法也会返回空字符串。
任何想法来解决这个错误?
以下是Application_Start中的RouteConfiguration,
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional });