什么是ASP.NET MVC的核心相当于Request.RequestURI?

编程入门 行业动态 更新时间:2024-10-20 09:21:13
本文介绍了什么是ASP.NET MVC的核心相当于Request.RequestURI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现的博客文章演示如何垫片熟悉的事物像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?

本文发布于:2023-11-03 15:31:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   NET   ASP   MVC   RequestURI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!