Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResolveUrl/ResolveClientUrl equivalents for Asp.Net Razor?

Tags:

So I've started using the <%: Url.Content(~/site/blah) %> syntax as standard for CSS, JScript and Image urls - solves a lot of issues indeed; and it's at least consistent beween WebForms and Razor pages (not all of my devs will be doing Razor, and yet they will still be working on this platform I've produced).

However, for something that I'm doing at the moment I could really do with a way to take a relative Url written in a Razor page and, at run time, resolve it to the correct server side file, before turning it back into an absolute Url for the client. Url.Content doesn't do anything with relative Urls.

So, basically, I want either an equivalent of ResolveUrl or ResolveClientUrl at the Razor level.

I would like this to enable terser (and more tolerant to renaming) resource paths in some of my MVC views, which can be a few folders further down from the root, and whose content folder would be more easily expressed as a relative path - so I could have:

folder\folder\views\shared\layout.cshtml
and
folder\folder\content\site.css

(I've inferred the use of a layout page, also, to mirror the kind of issues that are addressed by ResolveUrl and the re-basing that WebForms does)

Using Url.Content as it is, I would need to specify the full path:

Url.Content("~/folder/folder/content/site.css")

But what I would like is

Url.Content("../../site.css")

And have that work, of course, regardless of how many paths there are in the current request's route.

Of course I can get this to work in WebForms, if I ditch the Url.Content call and just rely on url rebasing.

Is there any equivalent in Razor?

like image 317
Andras Zoltan Avatar asked Feb 16 '11 17:02

Andras Zoltan


People also ask

Are razor pages Mvvm?

Razor Pages is sometimes described as implementing the MVVM (Model, View ViewModel) pattern. It doesn't. The MVVM pattern is applied to applications where the presentation and model share the same layer. It is popular in WPF, mobile application development, and some JavaScript libraries.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

Does ASP NET MVC use razor?

Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

What is @model in razor?

The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.


2 Answers

You could try using WebPageRenderingBase.Href method like this

@Href("../../site.css") 
like image 189
Alexander Prokofyev Avatar answered Sep 28 '22 06:09

Alexander Prokofyev


If you are converting ASP.NET application ASP.NET MVC step by step, you can use wrappers:

public abstract class ResolveClientUrlWrapper {     public abstract string ResolveClientUrl(string relativeUrl); }  public class ResolveClientUrlPageWrapper : ResolveClientUrlWrapper {     System.Web.UI.Page _page;      public ResolveClientUrlPageWrapper(System.Web.UI.Page page)     {             _page = page;     }      public override string ResolveClientUrl(string relativeUrl)     {         return _page.ResolveClientUrl(relativeUrl);     } }  public class ResolveClientUrlPWebPageRenderingBaseWrapper : ResolveClientUrlWrapper {     WebPageRenderingBase _webPageRenderingBase;      public ResolveClientUrlPWebPageRenderingBaseWrapper(WebPageRenderingBase webPageRenderingBase)     {         _webPageRenderingBase = webPageRenderingBase;     }      public override string ResolveClientUrl(string relativeUrl)     {         return _webPageRenderingBase.Href(relativeUrl);     } }  public class ResolveClientUrlUrlHelperWrapper : ResolveClientUrlWrapper {     UrlHelper _urlHelper;      public ResolveClientUrlUrlHelperWrapper(UrlHelper urlHelper)     {         _urlHelper = urlHelper;     }      public override string ResolveClientUrl(string relativeUrl)     {         return _urlHelper.Content(relativeUrl);     } }  public class PathUtilityWrapper : ResolveClientUrlWrapper {     public override string ResolveClientUrl(string relativeUrl)     {         return VirtualPathUtility.ToAbsolute(relativeUrl);     } } 

You can ResolveClientUrlPageWrapper from classic ASP.NET page or ResolveClientUrlHttpServerUtilityBaseWrapper from ASP.NET MVC controller, ResolveClientUrlUrlHelperWrapper from ASP.NET MVC View. You will have the same background code accross the whole ASP.NET classic and ASP.NET MVC application.

like image 37
Tomas Kubes Avatar answered Sep 28 '22 05:09

Tomas Kubes