目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造System.Web.Mvc.UrlHelper类。
using System;using System.Configuration;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace RetailCustomerInsight.Web.Utils{ ////// MVC URL帮助类,在ASP.NET 非MVC环境中构造MVC的URL信息 /// public static class MVCUrlHelper { ////// 根据ActionName构造MVC的URL /// /// MVC控制器中的ActionName ///MVC的URL public static string Action(string actionName) { var route = new RouteData();//构造一个空的RouteData,表示当前Http上下文中不存在MVC的上下文信息(即当前Request请求的URL信息不能提供是在MVC的哪个Controller中,也不能提供是在Controller下的哪个Action中) RequestContext requestContext = new RequestContext(new HttpContextWrapper(HttpContext.Current), route); UrlHelper url = new UrlHelper(requestContext); return url.Action(actionName); } ////// 根据ActionName和路由参数构造MVC的URL /// /// MVC控制器中的ActionName /// 路由参数 ///MVC的URL public static string Action(string actionName, object routeValues) { var route = new RouteData();//构造一个空的RouteData,表示当前Http上下文中不存在MVC的上下文信息(即当前Request请求的URL信息不能提供是在MVC的哪个Controller中,也不能提供是在Controller下的哪个Action中) RequestContext requestContext = new RequestContext(new HttpContextWrapper(HttpContext.Current), route); UrlHelper url = new UrlHelper(requestContext); return url.Action(actionName, routeValues); } ////// 根据ActionName和控制器名构造MVC的URL /// /// MVC控制器中的ActionName /// 控制器名 ///MVC的URL public static string Action(string actionName, string controllerName) { var route = new RouteData();//构造一个空的RouteData,表示当前Http上下文中不存在MVC的上下文信息(即当前Request请求的URL信息不能提供是在MVC的哪个Controller中,也不能提供是在Controller下的哪个Action中) RequestContext requestContext = new RequestContext(new HttpContextWrapper(HttpContext.Current), route); UrlHelper url = new UrlHelper(requestContext); return url.Action(actionName, controllerName); } ////// 根据ActionName、控制器名和路由参数构造MVC的URL /// /// MVC控制器中的ActionName /// 控制器名 /// 路由参数 ///MVC的URL public static string Action(string actionName, string controllerName, object routeValues) { var route = new RouteData();//构造一个空的RouteData,表示当前Http上下文中不存在MVC的上下文信息(即当前Request请求的URL信息不能提供是在MVC的哪个Controller中,也不能提供是在Controller下的哪个Action中) RequestContext requestContext = new RequestContext(new HttpContextWrapper(HttpContext.Current), route); UrlHelper url = new UrlHelper(requestContext); return url.Action(actionName, controllerName, routeValues); } }}
再来看看如何根据URL反向匹配出ContollerName和ActionName
using System.IO;using System.Web;using System.Web.Routing;namespace Daimler.CdnMgmt.Web.Utils{ ////// MVC路由的Controller和Acion /// public class ControllerActionValue { public string ActionName; public string ControllerName; } ////// 根据URL获取匹配MVC路由的Controller和Acion的帮助类 /// public static class HttpRouteParser { ////// 根据URL获取匹配MVC路由的Controller和Acion /// /// 要解析Controller和Acion的URL ///匹配MVC路由Controller和Acion的对象 public static ControllerActionValue GetControllerActionFromUrl(string url) { var conroller = string.Empty; var action = string.Empty; var resolveFlag = false; var hr = new HttpRequest("", url, ""); TextWriter stringWriter = new StringWriter(); var hrs = new HttpResponse(stringWriter); var hc = new HttpContext(hr, hrs); var hcw = new HttpContextWrapper(hc); foreach (var routeBase in RouteTable.Routes) { var r = (Route) routeBase; var rt = r.GetRouteData(hcw); if (rt != null) { resolveFlag = true; conroller = rt.Values["Controller"].ToString(); action = rt.Values["Action"].ToString(); break; } } if (resolveFlag) return new ControllerActionValue {ControllerName = conroller, ActionName = action}; return null; } }}