在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 ...
随机推荐
- TCP协议中的三次握手和四次挥手
转自: http://blog.csdn.net/whuslei/article/details/6667471/ 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示:
- leetcode:Valid Parentheses
括号匹配 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- Progress Reporting
Progress reporting is a key activity of project management. The project manager issues regular repor ...
- 删除docker私有库镜像
不断往私库里push image,发现里面大多数镜像已经版本过旧,用不到了,所以决定删除私库里那些没用的镜像. Docker registry默认提供了一个仓库清理的url,如下:可以删除镜像ubun ...
- Eclipse常用设置(转)
http://jingyan.baidu.com/article/3065b3b6efa9d7becff8a4c6.html 用惯了VS,再回过去用Eclipse真是一件痛苦的事.so,在这里记录下使 ...
- 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群
高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...
- 禁用LMHOSTS和NetBIOS后提升上网速度 ?
LMHOSTS 文件是 windows 中进行 netbios 静态解析时使用的,其作用类型于 HOSTS 文件. 今天发现这个东西有点问题,在用ADSL上网时,明明 IP . DNS 都设置得好好的 ...
- Android --LoginActivity模板登录
Android Studio使用自带LoginActivity模板,制作登录界面 登录界面功能: 1.记住表单账户密码,并自动登录 //获得sp实例对象 sp = this.getSharedPref ...
- [HDF]hdf-4.2.6类库的使用
HDF文件包括科学数据和VData部分.读取HDF格式的calipso数据,用GDAL可以方便的读取其中的子数据集,但是没有发现GDAL中提供读取Vdata的方法.所以只好考虑借助hdf-4.2.6类 ...
- Google File System翻译(转)
摘要 我们设计实现了google文件系统,一个面向大规模分布式数据密集性应用的可扩展分布式文件系统.它运行在廉价的商品化硬件上提供容错功能,为大量的客户端提供高的整体性能. 尽管与现有的分布式文件系统 ...