在MVC 5中,我具有以下扩展方法来生成绝对URL,而不是相对URL:
public static class UrlHelperExtensions
{
public static string AbsoluteAction(
this UrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
public static string AbsoluteContent(
this UrlHelper url,
string contentPath)
{
return new Uri(url.RequestContext.HttpContext.Request.Url, url.Content(contentPath)).ToString();
}
public static string AbsoluteRouteUrl(
this UrlHelper url,
string routeName,
object routeValues = null)
{
string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
return url.RouteUrl(routeName, routeValues, scheme);
}
}
ASP.NET Core中的等效项是什么?
UrlHelper.RequestContext
不再存在。
您不再拥有HttpContext
,因为不再有静态的HttpContext.Current
属性。
据我所知,您现在还需要传入HttpContext
或HttpRequest
对象。我对吗?有什么方法可以控制当前请求?
我是否在正确的轨道上,现在域是否应该是一个环境变量,可以简单地附加到相对URL?这会是更好的方法吗?
参考方案
在RC2和1.0之后,您不再需要为扩展类注入IHttpContextAccessor
。它可通过IUrlHelper
立即在urlhelper.ActionContext.HttpContext.Request
中使用。然后,您将遵循相同的思想创建一个扩展类,但由于不涉及注入,因此更简单。
public static string AbsoluteAction(
this IUrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = url.ActionContext.HttpContext.Request.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
留下有关如何构建它的细节,以防万一它们对某人有用。您可能也只对当前请求的绝对URL感兴趣,在这种情况下,请查看答案的结尾。
您可以修改扩展类以使用IHttpContextAccessor
接口获取HttpContext
。一旦有了上下文,就可以从HttpRequest
获取HttpContext.Request
实例,并使用其属性Scheme
,Host
,Protocol
等,如下所示:
string scheme = HttpContextAccessor.HttpContext.Request.Scheme;
例如,您可能需要使用HttpContextAccessor配置您的类:
public static class UrlHelperExtensions
{
private static IHttpContextAccessor HttpContextAccessor;
public static void Configure(IHttpContextAccessor httpContextAccessor)
{
HttpContextAccessor = httpContextAccessor;
}
public static string AbsoluteAction(
this IUrlHelper url,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = HttpContextAccessor.HttpContext.Request.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
....
}
您可以在Startup
类(Startup.cs文件)上执行以下操作:
public void Configure(IApplicationBuilder app)
{
...
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
UrlHelperExtensions.Configure(httpContextAccessor);
...
}
您可能想出了不同的方法来在扩展类中获取IHttpContextAccessor
,但是,如果最终想要将方法保留为扩展方法,则需要将IHttpContextAccessor
注入到静态类中。 (否则,每次调用时都需要IHttpContext
作为参数)
仅获取当前请求的absoluteUri
如果只想获取当前请求的绝对uri,则可以使用GetDisplayUrl
类中的扩展方法GetEncodedUrl
或UriHelper
。 (与UrLHelper不同)
GetDisplayUrl。以完全不转义的形式返回查询URL的组合组件(QueryString除外),仅适用
用于显示。 HTTP标头或其他格式中不应使用此格式
HTTP操作。
GetEncodedUrl。以完全转义的形式返回请求URL的组合组件,适合在HTTP标头和其他标头中使用
HTTP操作。
为了使用它们:
包括名称空间Microsoft.AspNet.Http.Extensions
。
获取HttpContext
实例。它在某些类(例如剃刀视图)中已经可用,但是在其他类中,您可能需要如上所述插入IHttpContextAccessor
。
然后像this.Context.Request.GetDisplayUrl()
一样使用它们
这些方法的替代方法是使用HttpContext.Request
对象中的值手动制作绝对uri(类似于RequireHttpsAttribute的操作):
var absoluteUri = string.Concat(
request.Scheme,
"://",
request.Host.ToUriComponent(),
request.PathBase.ToUriComponent(),
request.Path.ToUriComponent(),
request.QueryString.ToUriComponent());
使用ASP.NET MVC中的按钮来处理通过页面进行操纵的正确方法? - c#我正在使用ASP.NET MVC编写一个应用程序,出于各种原因,其中的导航是通过HTML输入按钮来处理的。如何处理这种情况的最佳做法是什么?在需要发布信息的按钮上设置表单,然后使用JavaScript重定向不需要保留信息的按钮通过表单处理所有按钮,直至迷你表单处理不需要保留信息的按钮上的导航<% using (Html.BeginForm()) { %…
使用ASP.NET MVC在View的EditorFor()方法中显示ViewBag的值 - c#这可能是我的语法错误,但是我无法在我的视图中使用EditorFor为ViewBag设置默认值。我检查了ViewBag.FirstName中的值是否正确传递,这很好。但是,该字段显示为无值。我的声明是:@Html.EditorFor(model => model.Person.FirstName, new { htmlAttributes = new {…
从JAVA调用方法C#.NET - java我有一个C#.NET项目中创建的dll,我想从Java程序中调用方法。我想知道是否存在实现此目标的机会,然后遇到了JNA和JNI。我应该使用哪一个?有什么建议么?我只需要在使用C#.NET编写的类中调用方法并处理Java程序的结果即可。 参考方案 这取决于您的应用程序,但是您可以将C#DLL放在服务中,例如WCF并以这种方式将功能公开给Java代码。使用ws…
从ASP.NET 1.1转换为ASP.Net 3.5后,单选按钮停止工作 - javascript[我在下面以FYI的形式发布我们的问题和解决方案。如果您有Microsoft或代表机构对此问题的正式声明,我会将其标记为答案。否则,我下面的帖子将作为答案]。问题:我们采用了ASP.Net 1.1程序,并将其转换为ASP.Net 3.5。该页面最初正确显示,默认情况下选中一个单选按钮,并显示一个项目列表(“列表A”)。当我们单击另一个单选按钮时,它将正确显示…
ddl在服务器中未更新-asp.net - javascript我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……