Redirect asp.net mvc page from business logic class

有些话、适合烂在心里 提交于 2019-12-23 08:49:07

问题


I am calling a static method within my business logic layer that, for purposes I won't mention here, needs to do the redirecting itself rather returning information back to the controller to do the redirect.

I figure I need to use the HttpContext object but am struggling with creating the route. I can't simply do context.Response.Redirect("someController/someMethod) because I need to include parameters for the action controller that I"m sending the user to.

Assuming this is correct:

HttpContext context = HttpContext.Current;

Can anyone please provide some syntax help with how to create a route using an object like:

new { Controller = "MyController", action = "Index", OtherParm="other value" }

TIA


回答1:


Very ugly, anti-MVC, don't do in business layer, etc... but since you are asking:

var context = new RequestContext(
    new HttpContextWrapper(System.Web.HttpContext.Current), 
    new RouteData());
var urlHelper = new UrlHelper(context);
var url = urlHelper.Action("Index", new { OtherParm = "other value" });
System.Web.HttpContext.Current.Response.Redirect(url);


来源:https://stackoverflow.com/questions/2066351/redirect-asp-net-mvc-page-from-business-logic-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!