asp.net – 从Mvc Action中获取WebApi UrlHelper的实例
发布时间:2020-12-16 09:24:44 所属栏目:asp.Net 来源:网络整理
导读:我在同一个项目中运行WebApi和Mvc(所以它们在进程中). Mvc主要用于服务资产(页面和生成的下载)以及用于ajax数据请求的web api. 为了成为RESTish,大多数WebApi请求都包含一组由以下类生成的链接: public class ApiLinkMaker{ public ApiLinkMaker(UrlHelper
我在同一个项目中运行WebApi和Mvc(所以它们在进程中). Mvc主要用于服务资产(页面和生成的下载)以及用于ajax数据请求的web api.
为了成为RESTish,大多数WebApi请求都包含一组由以下类生成的链接: public class ApiLinkMaker { public ApiLinkMaker(UrlHelper url,string authority) { this.url = url; this.authority = authority; } public ApiLinkMaker(ApiController controller) : this(controller.Url,controller.Request.RequestUri.Authority) { } public string MakeLink(string controller,string id) { return "//" + authority + url.Route("DefaultApi",new { controller = controller,id = id }); } } 那里还有其他一些方法,但这确实是事情的核心,它运作良好. 现在我想优化特定页面.以前我有两个请求 >下载html 现在我意识到,出于优化目的,在这种情况下最好只做一个. >下载嵌入了JSON的数据的html. 问题是,由于html是由Mvc生成的,我无法创建一个似乎有用的Api UrlHelper. 我试过了 var url = new UrlHelper(new HttpRequestMessage(verb,controller.Request.Url.AbsoluteUri)); if (!url.Request.Properties.ContainsKey(HttpPropertyKeys.HttpConfigurationKey)) //https://stackoverflow.com/questions/11053598/how-to-mock-the-createresponset-extension-method-on-httprequestmessage url.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey,new HttpConfiguration()); 但这仍然会爆炸 System.ArgumentException was unhandled by user code HResult=-2147024809 Message=A route named 'DefaultApi' could not be found in the route collection. Parameter name: name Source=System.Web.Http ParamName=name StackTrace: at System.Web.Http.HttpRouteCollection.GetVirtualPath(HttpRequestMessage request,String name,IDictionary`2 values) at System.Web.Http.Routing.UrlHelper.GetHttpRouteHelper(HttpRequestMessage request,String routeName,IDictionary`2 routeValues) at System.Web.Http.Routing.UrlHelper.GetHttpRouteHelper(HttpRequestMessage request,Object routeValues) at System.Web.Http.Routing.UrlHelper.Route(String routeName,Object routeValues) at MyProject.Models.ApiLinkMaker.MakeLink(String controller,String id) in w:MyProjectModelsApiLinkMaker.cs:line 42 ... 这让我觉得我错了 – 我需要以某种方式从api路由配置创建url帮助器. 解决方法
为什么创建一个?在MVC Controller和ApiController类上都有一个UriHelper实例作为属性公开.
public ActionResult Index() { string url = Url.RouteUrl("DefaultApi",new {httproute = "",controller = "test"}); return View(); } 编辑:更新的代码.虽然url助手不同,但您可以使用MVC url助手来解析Web api url. Edit2:如果你想从Mvc UrlHelper获取webapi路由,使用正确的方法是 string url = Url.HttpRouteUrl("DefaultApi",controller = "test"}); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在ASP.NET中隐藏页面名称和查询字符串?
- kendo-ui – 如何使用它的ASP MVC Complete Wrapper设置Ken
- 如何将组名应用于asp.net中的HTML单选按钮?
- ASP.Net – 在没有Windows用户的情况下使用基本身份验证
- asp.net – 覆盖webapi odata链接的主机
- asp.net-mvc – 在ASP.NET MVC ContactsManager教程中是否有
- asp.net – 设置身份验证表单loginUrl动态?
- asp.net – 当用户有多个角色时,位置授权如何工作?
- asp.net-mvc-4 – 什么是antlr3,为什么默认情况下在VS2012
- asp.net – 在Sitecore中检索URL路径部分的方法是什么?
推荐文章
站长推荐
- IIS 8发布ASP.NET核心应用程序 – 正在使用的文件
- AspNetCore.Authentication.JwtBearer失败,没有S
- 我如何开始用asp.net学习jquery?
- asp.net-mvc-3 – 如何将ObjectResult转换为IQue
- asp.net-mvc-2 – 阅读OAuth2.0 Signed_Request
- asp.net-mvc – 将动态JSON对象传递给Web API –
- asp.net-mvc – Angular 2中的组件是什么
- asp.net-mvc – 如何在ASP.NET MVC中阻止JSON序列
- asp.net-mvc – 列表中的MVC4绑定下拉列表(bug)
- asp.net – web.config urlmapping
热点阅读