VirtualPathUtility的ASP.Net核心替换 [英] ASP.Net Core Replacement for VirtualPathUtility
问题描述
ASP.Net Core中是否可以替代 VirtualPathUtility.ToAbsolute
?它似乎不可用.
Is there a replacement for VirtualPathUtility.ToAbsolute
in ASP.Net Core? It doesn't seem to be available.
我想转换相对路径,例如〜/bob"进入绝对路径,例如"/app/bob".
I'm wanting to convert a relative path e.g. "~/bob" into an absolute path, e.g. "/app/bob".
我正在尝试从类库中执行此操作,因此无法访问控制器和视图的常规属性.
I'm trying to do this from a class library and so doesn't have access to the usual properties of controllers and views.
我正在将应用程序从ASP.Net移植到ASP.Net Core,并且该库在很多地方都被调用,而从控制器中传递参数并不是我想要做的事情-可能要花费数小时的时间由于必须要经过的地方数量而无法工作.
I'm porting an app from ASP.Net to ASP.Net Core and this library is being called from lots of places and passing parameters through from the controller is not something that I want to be doing - it would be hours of work due to number of places I would have to pass everything through.
我正在使用在.Net Framework 4.7.1上运行的ASP.Net Core 2.1(尽管我计划在将来的版本中移植到.Net Core,但是一旦删除了一些依赖项,我希望有一个可行的解决方案与Famework和.Net Core一起使用)
I'm using ASP.Net Core 2.1, running on .Net Framework 4.7.1 (although I plan to port to .Net Core in a future release, once some dependencies have been removed so I want a solution that will work with Famework and .Net Core)
推荐答案
我做了什么:
已安装 Microsoft.ApplicationInsights.AspNetCore
.现在,我有了 GetUri()
扩展名:
Installed Microsoft.ApplicationInsights.AspNetCore
.
Now I had GetUri()
extension avaliable:
var originalUri = myController.HttpContext.Request.GetUri();
var url = "~/blablabla/toto.png";
var absoluteUrl = url.Replace("~/", $@"{originalUri.Scheme}://{originalUri.Authority}/");
这篇关于VirtualPathUtility的ASP.Net核心替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!