开发者

Is there a way to use RedirectToAction or something equivalent in a ASP.Net Forms page?

开发者 https://www.devze.com 2023-02-16 02:46 出处:网络
I have a mixed environment with both Forms and MVC v2 pages.I want to redirect to a MVC page from a Forms page.

I have a mixed environment with both Forms and MVC v2 pages. I want to redirect to a MVC page from a Forms page.

Usually I use RedirectToAction when within the controller. But when I am in a Asp.Net Forms type page I can't really 开发者_如何学Gouse that.

I'm digging around the framework to find a solution, thought I would ask here in case someone already knows the answer to this.


I found the answer on my own. Here is how I solved this:

var controllerName = "NameOfMyController";
var actionName = "NameOfMyAction";

//Lets resolve the URL for the controller/action based on the existing routes
var routes = RouteTable.Routes;
var virtualPathData = routes.GetVirtualPath(
                        null,
                        new RouteValueDictionary {
                                {"Controller", controllerName}, 
                                {"Action", actionName}
                        });

/* resolvedUrl should now contain the full URL to 
the controller/action defined at the beginging */
var resolvedUrl =  virtualPathData == null ? "" : virtualPathData.VirtualPath;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号