asp.net-mvc – 从Api控制器内生成绝对的url to action
发布时间:2020-12-30 22:11:04 所属栏目:asp.Net 来源:互联网
导读:我使用asp.net mvc4和我在“controller1”这个动作: [HttpGet] public async Taskstring Action1() { try { HttpClient cl = new HttpClient(); string uri = controller2/a
我使用asp.net mvc4和我在“controller1”这个动作: [HttpGet] public async Task<string> Action1() { try { HttpClient cl = new HttpClient(); string uri = "controller2/action2"; HttpResponseMessage response = await cl.GetAsync(uri); response.EnsureSuccessStatusCode(); return response.ToString(); } catch { return null; } } 当我将uri设置为“http:// localhost:1733 / controller2 / action2”时,动作正常,但是不要将uri设置为“controller2 / action2”或“/ controller2 / action2”或“/ controller2 / action2”. 如何在没有硬编码uri的情况下写这个动作? 谢谢. 解决方法使用:string uri = Url.Action("Action2","Controller2",new {},Request.Url.Scheme); 更新: 由于您使用API控制器并需要为常规控制器生成Url,所以您必须使用: string uri = this.Url.Link("Default",new { controller = "Controller2",action = "Action2" }); 其中Default是在注册路由集合中定义的路由的名称,或者如果您已经为此操作创建了特定路由,请使用其名称和新{}作为第二参数. 对于MVC版本4,请在/ App_Start / RoutesConfig.cs检查您注册的路由.对于MVC版本3,请在您的Global.asax中检查您的RegisterRoutes方法. (编辑:哈尔滨站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 经过身份验证的服务不支持跨域javascript回调.
- asp.net-mvc – 是否有一个ASP MVC与JSTL标签等效?
- asp.net-mvc – UpdateModel前缀 – ASP.NET MVC
- asp.net-web-api – Web Api:找不到System.Net.Http版本2.
- asp.net – Web部署安装程序(MSI)中没有“IIS 7部署处理程序
- asp.net-mvc-3 – 依赖注入与多个类实现的接口
- asp.net – compilation debug =“true”和发布模式“relea
- asp.net-mvc-3 – 在ASP.NET MVC 3中覆盖/禁用授权
- asp.net-mvc – 在ASP.NET MVC中实现工作单元的方法
- asp.net-mvc-3 – 如何在Razor视图引擎中注册程序集
推荐文章
站长推荐
- asp.net-mvc-2 – 使用’class(或其他保留关键字
- asp.net – ASP MVC – 默认的HTTP标头有任何常量
- asp.net – 为什么Global.asax事件在我的ASP.NET
- asp.net-mvc – 使用IIS7.5将网站中的URL绑定到另
- asp.net-mvc – ASP.NET MVC jQueryUI datepicke
- 在asp.net c#应用程序中使用Graphviz Dll
- ASP.NET批量下载文件的方法
- asp.net – 按钮属性: – commandName和Command
- 在asp.net App_Code目录中使用Nemerle
- 如何保护我的ASP.NET AJAX应用程序?
热点阅读