Nul*_*ter 26 asp.net-mvc asp.net-mvc-2-validation asp.net-mvc-3 asp.net-mvc-4 asp.net-mvc-5
在我的Web应用程序中,我正在验证来自glabal.asax的URL.我想验证网址,如果需要,还需要重定向到某个操作.我正在使用Application_BeginRequest来捕获请求事件.
protected void Application_BeginRequest(object sender, EventArgs e)
{
// If the product is not registered then
// redirect the user to product registraion page.
if (Application[ApplicationVarInfo.ProductNotRegistered] != null)
{
//HOW TO REDIRECT TO ACTION (action=register,controller=product)
}
}
Run Code Online (Sandbox Code Playgroud)
或者是否有任何其他方法来验证每个URL,同时在mvc中获取请求并在需要时重定向到操作
Afa*_*zal 25
以上所有内容都不起作用,您将处于执行方法的循环中Application_BeginRequest.
你需要使用
HttpContext.Current.RewritePath("Home/About");
Run Code Online (Sandbox Code Playgroud)
Nul*_*ter 24
使用以下代码进行重定向
Response.RedirectToRoute("Default");
Run Code Online (Sandbox Code Playgroud)
"默认"是路由名称.如果要重定向到任何操作,只需创建路由并使用该路由名称.
Nel*_*ari 11
除了已经提到的方式.另一种方法是使用URLHelper,我在场景中使用过一次错误,用户应该重定向到Login页面:
public void Application_PostAuthenticateRequest(object sender, EventArgs e){
try{
if(!Request.IsAuthenticated){
throw new InvalidCredentialException("The user is not authenticated.");
}
} catch(InvalidCredentialException e){
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Response.Redirect(urlHelper.Action("Login", "Account"));
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做:
HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Home");
routeData.Values.Add("action", "FirstVisit");
IController controller = new HomeController();
RequestContext requestContext = new RequestContext(contextWrapper, routeData);
controller.Execute(requestContext);
Response.End();
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以包装传入的请求上下文并将其重定向到其他地方,而无需重定向客户端。因此重定向不会触发 global.asax 中的另一个 BeginRequest。
| 归档时间: |
|
| 查看次数: |
51168 次 |
| 最近记录: |