目前项目中有个需求,需要在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;
}
}
}

最新文章

  1. POJ2155 Matrix二维线段树经典题
  2. NSObject的load和initialize方法(转)
  3. windows tomcat配置大全
  4. Get current time and date on Android
  5. c#_错误处理_基础
  6. Android安全问题 钓鱼程序
  7. C++域宽设置
  8. [TypeScript] Using Lodash in TypeScript with Typings and SystemJS
  9. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高
  10. linux 分区
  11. CDT+Eclipse代码自动提示
  12. JS操作URL
  13. JTextField限制输入长度的完美解决方案(转)
  14. 使用(Drawable)资源——图片资源
  15. redis单机安装以及简单redis集群搭建
  16. Unity引擎与C#脚本简介
  17. 第二周Access课总结
  18. 1474 十进制转m进制
  19. 【PyQt5-Qt Designer】界面布局
  20. 4+1视图与UML对应关系

热门文章

  1. Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select
  2. A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.
  3. 二代身份证阅读器(XZX)
  4. The include feature of SQL Server Index
  5. js 事件监听封装
  6. 前端开发者需要的10个Sublime插件
  7. git log 常用命令
  8. 前端学习——css实用技术
  9. Java I/O解读与使用实例
  10. Error : L6218E: Undefined symbol downloadAddress (referred from nand.o).