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

在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数的更多相关文章

  1. Mybatis在非spring环境下配置文件中使用外部数据源(druidDatasource)

    Spring环境下, MyBatis可以通过其本身的增强mybatis-spring提供的org.mybatis.spring.SqlSessionFactoryBean来注入第三方DataSourc ...

  2. 开发中少不了的Fun -- 获取地址栏URL参数

    假设这是一个url地址 http://localhost:8080/a/b/c?a=1&b=2#abc,里面包含的部分: protocol: 'http:', // 协议 host: 'loc ...

  3. webform 中使用ajax

    常用的方式有 js –> WebService  , js->*.ashx, js->WebAPI, js->MVC Controller->Action. 前两种就不说 ...

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

    目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...

  5. 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

    原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...

  6. Asp.net中WebForm 与 MVC的架构区别

    ASP.NET Webform 后台代码(behind code) 这种behind code 模式有5个问题,我们用MVC的设计思想来分别解决这些问题 1.基于视图的方案来解决基于行为的需求 从上图 ...

  7. ASP.NET中HttpApplication中ProcessRequest方法中运行的事件顺序;ASP.NET WebForm和MVC总体请求流程图

    ASP.NET中HttpApplication中ProcessRequest方法中运行的事件顺序 1.BeginRequest  開始处理请求 2.AuthenticateRequest 授权验证请求 ...

  8. ASP.NET MVC IOC 之 Autofac(三)-webform中应用

    在webform中应用autofac,只有global中的写法不一样,其他使用方式都一样 nuget上引用: global中的写法: private void AutoFacRegister() { ...

  9. 在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录

    在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录 前言 最近有个Web项目,业务功能不复杂,但是这个客户(某政府部门)有两个硬性要求:1 ...

随机推荐

  1. [LeetCode]题解(python):031-Next Permutation

    题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges nu ...

  2. Selenium2学习-015-WebUI自动化实战实例-013-通过 URL 关闭多余的已开浏览器窗口

    在日常的 WebUI 自动化测试脚本执行的过程中,经常会打开不同的网页,进行相应的操作,此时可能会打开很多的网页,当打开的网页过多时,无效的网页资源对运行脚本的机器造成了过多无效的资源浪费,因而在日常 ...

  3. Java学习-011-创建文件实例及源代码

    此文源码主要为应用 Java 创建文件的源代码.若有不足之处,敬请大神指正,不胜感激! 创建文件的源代码如下所示: /** * @function 文件操作:创建文件.若文件不存在,则直接创建文件:若 ...

  4. 忘记Mysql的root密码的处理办法

    1,停止MYSQL服务,CMD打开DOS窗口,输入 net stop mysql 2,在CMD命令行窗口,进入MYSQL安装目录 比如E:\Program Files\MySQL\MySQL Serv ...

  5. div滑入与滑出

    html <div class="pop_tit"> <span class="p_tit1" title="大连未来城LECITY ...

  6. 8款实用Sublime text 3插件推荐

    Sublime Text作为一个尽为人知的代码编辑器,其优点不用赘述.界面整洁美观.文本功能强大,且运行速度极快,非常适合编写代码,写文章做笔记.Sublime Text还支持Mac.Windows和 ...

  7. sql语句like多个条件的写法实例

    这篇文章介绍了sql语句like多个条件的写法实例,有需要的朋友可以参考一下 //   表A  no name 1   lu,li,zhang  2   zhou,wei,liu  3   li,fa ...

  8. Nuget程序包 使用log4net

    Nuget程序包不用细介绍,网上资源很多,有个项目使用了log4net,为项目打log,功能很多,足够一般使用. 使用时候需要在配置文件中对其进行相关配置,我自己的配置文件放在App.config文件 ...

  9. 点云匹配和ICP算法概述

    Iterative Closest Point (ICP) [1][2][3] is an algorithm employed to minimize the difference between ...

  10. iOS UICollectionView之二(垂直滚动)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...