在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数,以及如何根据URL解析出匹配到MVC路由的Controller和Action

目前项目中有个需求,需要在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
{
/// <summary>
/// MVC URL帮助类,在ASP.NET 非MVC环境中构造MVC的URL信息
/// </summary>
public static class MVCUrlHelper
{
/// <summary>
/// 根据ActionName构造MVC的URL
/// </summary>
/// <param name="actionName">MVC控制器中的ActionName</param>
/// <returns>MVC的URL</returns>
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);
} /// <summary>
/// 根据ActionName和路由参数构造MVC的URL
/// </summary>
/// <param name="actionName">MVC控制器中的ActionName</param>
/// <param name="routeValues">路由参数</param>
/// <returns>MVC的URL</returns>
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);
} /// <summary>
/// 根据ActionName和控制器名构造MVC的URL
/// </summary>
/// <param name="actionName">MVC控制器中的ActionName</param>
/// <param name="controllerName">控制器名</param>
/// <returns>MVC的URL</returns>
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);
} /// <summary>
/// 根据ActionName、控制器名和路由参数构造MVC的URL
/// </summary>
/// <param name="actionName">MVC控制器中的ActionName</param>
/// <param name="controllerName">控制器名</param>
/// <param name="routeValues">路由参数</param>
/// <returns>MVC的URL</returns>
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
{
/// <summary>
/// MVC路由的Controller和Acion
/// </summary>
public class ControllerActionValue
{
public string ActionName;
public string ControllerName;
} /// <summary>
/// 根据URL获取匹配MVC路由的Controller和Acion的帮助类
/// </summary>
public static class HttpRouteParser
{
/// <summary>
/// 根据URL获取匹配MVC路由的Controller和Acion
/// </summary>
/// <param name="url">要解析Controller和Acion的URL</param>
/// <returns>匹配MVC路由Controller和Acion的对象</returns>
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;
}
}
}
上一篇:thinkphp中URL传参数的几种方式


下一篇:java网络编程客户端与服务端原理以及用URL解析HTTP协议