我发现的博客文章演示如何垫片熟悉的事物像HttpResponseMessage回ASP.NET MVC的核心,但我想知道什么是应该做同样的事情,如下面的代码新的土办法在控制器的REST Post方法:
I found a blog post that shows how to "shim" familiar things like HttpResponseMessage back into ASP.NET Core MVC, but I want to know what's the new native way to do the same thing as the following code in a REST Post method in a Controller:
// POST audit/values [HttpPost] public System.Net.Http.HttpResponseMessage Post([FromBody]string value) { var NewEntity = _repository.InsertFromString(value); var msg = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Created); msg.Headers.Location = new Uri(Request.RequestUri + NewEntity.ID.ToString()); return msg; }在一个ASP.NET MVC的核心项目,我可以'不像是会得到Request.RequestUri。
In an ASP.NET Core MVC project, I can't seem to get Request.RequestUri.
我试过检查要求,我能够做出这样的函数:
I tried inspecting Request, and I was able to make a function like this:
private string UriStr(HttpRequest Request) { return Request.Scheme + "://" + Request.Host + Request.Path; // Request.Path has leading / }
所以,我可以写 UriStr(请求)来代替。但我不知道这是正确的。我觉得像我黑客攻击我的方式左右,而无法正常使用此。
So I could write UriStr(Request) instead. But I'm not sure that's right. I feel like I'm hacking my way around, and not using this correctly.
一个的有关问题早期非核心ASP.NET MVC的版本要求如何得到网站的基本URL。
A related question for earlier non-Core ASP.NET MVC versions asks how to get the base url of the site.
推荐答案一个更清洁的方法是使用 UriBuilder :
A cleaner way would be to use a UriBuilder:
private static Uri GetUri(HttpRequest request) { var builder = new UriBuilder(); builder.Scheme = request.Scheme; builder.Host = request.Host.Value; builder.Path = request.Path; builder.Query = request.QueryString.ToUriComponent(); return builder.Uri; }
(未测试,代码可能需要一些调整)
(not tested, the code might require a few adjustments)
更多推荐
什么是ASP.NET MVC的核心相当于Request.RequestURI?
发布评论