asp.net-mvc – 从Api控制器内生成绝对的url to action
发布时间:2020-12-15 20:18:01 所属栏目:asp.Net 来源:网络整理
导读:我使用asp.net mvc4和我在“controller1”这个动作: [HttpGet] public async Taskstring Action1() { try { HttpClient cl = new HttpClient(); string uri = "controller2/action2"; HttpResponseMessage response = await cl.GetAsync(uri); response.Ens
我使用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身份验证cookie仅在IE中消失,仅在特定位置消失
- 使用ASP.net和Access数据库保护SQL Injection的网站
- asp.net-mvc – 直接下载链接到ASP.NET MVC 5 for VS 2012
- asp.net – Request.IsAuthenticated永远不会成立
- asp.net – 使用WebMethods和session时的最佳实践
- asp.net – 403禁止错误
- asp.net – 有没有办法以编程方式设置Elmah中的应用程序名称
- asp.net-mvc – MVC 3中的区域全局过滤器
- asp.net – 使用SSL无法找到wcf服务的资源
- asp.net-web-api – Asp.net Web Api的基本项目模板?
推荐文章
站长推荐
热点阅读