asp.net-mvc – 如何在ASP.NET MVC中构建没有ViewContext的路由?
发布时间:2020-05-28 16:15:41 所属栏目:asp.Net 来源:互联网
导读:我想从一个无法访问ViewContext的实用程序类创建一个路由. 这可能吗?似乎没有任何等效的ViewContext.Current 我已尝试在Routing和HttpContext的所有构造函数中钓鱼,但无法完全达到我想要的效果. 这就是我正在寻找的 – 虽然这不起作用,因为RouteTable.Routes
|
我想从一个无法访问ViewContext的实用程序类创建一个路由. 这可能吗?似乎没有任何等效的ViewContext.Current 我已尝试在Routing和HttpContext的所有构造函数中钓鱼,但无法完全达到我想要的效果. 这就是我正在寻找的 – 虽然这不起作用,因为RouteTable.Routes的类型是RouteCollection而不是RouteData.如此接近 – 但到目前为止:-) RequestContext requestContext = new RequestContext(HttpContext.Current,RouteTable.Routes);
UrlHelper url = new UrlHelper(requestContext);
var urlString = url.RouteUrl(new {controller="DynamicImage",action="Button",text="Hello World"});
注意:RequestContest的类型为System.Web.Routing.RequestContext,而不是HttpContext 解决方法试试这个:var httpContext = new HttpContextWrapper(HttpContext.Current); var requestContext = new RequestContext(httpContext); var urlHelper = new UrlHelper(requestContext,new RouteData())); 希望这可以帮助 更新: 前一个是不正确的(我已经从我的记忆中发布了它).试试这个(它适用于我的一个项目): var httpContext = new HttpContextWrapper(HttpContext.Current); var requestContext = new RequestContext(httpContext,new RouteData()); var urlHelper = new UrlHelper(requestContext); new RouteData()仅用于RequestContext初始化,新的UrlHelper(requestContext)实际调用新的UrlHelper(requestContext,RouteTable.Routes) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 用Moq Mocking HttpContextBase
- 在ASP.NET中检测来自移动浏览器的请求
- IIS ASP.Net网站 – ManagedPipelineHandler错误(尝试对不存
- asp.net – 从DropdownList SelectedItem获取属性
- asp.net-mvc – 使用键入的HTML助手时的模型前缀
- asp.net – ‘WebForm_DoPostBackWithOptions’在IE11预览中
- asp.net – Visual Studio开发服务器使用错误的端口
- 如何在ASP.NET中的dropdownlist控件中处理长文本
- asp.net web-api – ASP.Net Web API模型绑定不工作像在MVC
- asp.net-mvc-3 – ViewBag在Extension Class中返回null
推荐文章
站长推荐
- asp.net – 在.NET .NET中ctl00是常量吗?
- asp.net-mvc – MVC3 Url.Action查询字符串生成
- asp.net – 对象不是垃圾回收,但不包含gcroot
- asp.net – 谷歌浏览器在浏览器关闭后保留会话变
- asp.net – 如何动态显示网站上的SVN修订版?
- asp.net-mvc – 在ASP.NET MVC中的NHibernate会话
- asp.net-mvc-3 – asp.net mvc 3 razor从IEnumer
- asp.net-mvc – 具有指定Controller和Action的Ht
- asp.net-mvc – 到Spark还是不Spark?
- asp.net-web-api – WebAPI 2属性路由启用会话状
热点阅读
