在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数
目前项目中有个需求,需要在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参数的更多相关文章
- Mybatis在非spring环境下配置文件中使用外部数据源(druidDatasource)
Spring环境下, MyBatis可以通过其本身的增强mybatis-spring提供的org.mybatis.spring.SqlSessionFactoryBean来注入第三方DataSourc ...
- 开发中少不了的Fun -- 获取地址栏URL参数
假设这是一个url地址 http://localhost:8080/a/b/c?a=1&b=2#abc,里面包含的部分: protocol: 'http:', // 协议 host: 'loc ...
- webform 中使用ajax
常用的方式有 js –> WebService , js->*.ashx, js->WebAPI, js->MVC Controller->Action. 前两种就不说 ...
- 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数,以及如何根据URL解析出匹配到MVC路由的Controller和Action
目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...
- 返璞归真 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 上传文件, ...
- Asp.net中WebForm 与 MVC的架构区别
ASP.NET Webform 后台代码(behind code) 这种behind code 模式有5个问题,我们用MVC的设计思想来分别解决这些问题 1.基于视图的方案来解决基于行为的需求 从上图 ...
- ASP.NET中HttpApplication中ProcessRequest方法中运行的事件顺序;ASP.NET WebForm和MVC总体请求流程图
ASP.NET中HttpApplication中ProcessRequest方法中运行的事件顺序 1.BeginRequest 開始处理请求 2.AuthenticateRequest 授权验证请求 ...
- ASP.NET MVC IOC 之 Autofac(三)-webform中应用
在webform中应用autofac,只有global中的写法不一样,其他使用方式都一样 nuget上引用: global中的写法: private void AutoFacRegister() { ...
- 在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录
在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录 前言 最近有个Web项目,业务功能不复杂,但是这个客户(某政府部门)有两个硬性要求:1 ...
随机推荐
- sqlserver 视图能否有变量
不能,sqlserver 视图一般不能有变量,也不能带存储过程
- 解决调用context.Session["NAME"]时总出现Object reference not set to an instance of an object.异常的方法
if (context.Session != null) { }
- boost.compressed_pair源码剖析
意义 当compressed_pair的某一个模板参数为一个空类的时候将对其进行“空基类优化”,这样可以使得compressed_pair占用的空间比std::pair的更小. 参考如下代码: #in ...
- LeetCode Longest Increasing Path in a Matrix
原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...
- LeetCode Dungeon Game
原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...
- APICloud请你看英特尔智能硬件大赛决赛直播
英特尔智能硬件大赛由英特尔硬享公社(CCE)发起,联合了全国各地50余家产业链优秀合作伙伴,旨在集合全国硬创资源,携手寻找中国最具代表性的硬件创业项目,并通过技术支持.资源对接.产品推广等方式助力项目 ...
- ucenter 通信成功后 apps.php无误后 该做的事
比如你的系统有个登陆功能,在本系统登陆之前要先执行如下代码,先把apps.php里的所有站点先登陆一遍 <meta charset="utf8"> <?php i ...
- Android设计模式源码解析之Builder模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/builder/mr. ...
- DLL项目报错:fatal error lnk1104: cannot open file "...\xxx.dll"
在生成DLL的时候报这个错, 生成不了DLL 检查生成DLL的路径是否有其他程序在使用... 或者把杀毒软件关了试试...
- Java基础之写文件——缓冲区中的多条记录(PrimesToFile3)
控制台程序,上一条博文(PrimesToFile2)每次将一个素数写入到文件中,所以效率不是很高.最好是使用更大的缓冲区并加载多个素数. 本例重复使用三个不同的视图缓冲区加载字节缓冲区并尽可能加入更多 ...