asp.net - 如何从 Asp.Net MVC 中的 Context.Request 获取 BaseUrl?

标签 asp.net .net asp.net-mvc asp.net-mvc-4

给出如下请求:

 http://www.sitename.com/controller/action/2

我正在尝试使用 Context.Request.Url 方法之一提取 http://www.sitename.com。我试过 AbsoluteUrl 和其他一些,但没有任何运气。是否有任何内置方法可以实现此目的,还是我必须编写自己的实用程序函数?

最佳答案

HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, 
                                              UriFormat.SafeUnescaped)

这将为您提供 http 或 https 以及给定 URL 的域。根据您使用此方法的位置,HttpContext.Current 可能是不必要的。

关于asp.net - 如何从 Asp.Net MVC 中的 Context.Request 获取 BaseUrl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19757884/

相关文章:

asp.net - 关注 TabContainer 内的 TextBox

asp.net - 在 Internet Explorer 中打开文档时如何将 MS Word 模板文件 (dotx) 保存为 docx

c# - java 和 Wp7 中的压缩和加密不匹配

c# - 确定事件处理程序的优先级

c# - Entity Framework System.InvalidOperationException : 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker.'

c# - 如何修复 C# 中的 'Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack' 错误

c# - 使用表优化中继器的 asp.net C# 代码

asp.net - 在没有 IIS 和 VS 2017 的情况下使用 Kestrel

c# - 如何将列表字符串传递给 TempData Asp.Net

asp.net-mvc - 即使在有效的 ViewResults 上,Machine.Specifications.MVC ShouldBeAView() 也总是失败