...">

What is the right and easy way to handle content URLs in ASP.NET MVC?

First we have the form:

<link href="../../../content/somecontent" />

It is annoying to find out (you need to calculate the depth of the path one by one) and are most prone to errors. Someone came up with this:

<link runat="server" href="~/content/somecontent" />

It is simpler, but I don’t know how randomly I can use this solution. Can it be used anywhere? Does it work with Spark? How does this affect rendering speed? And the last and worst resort will be:

<link href="/content/somecontent" />

This prevents the web application from being in a subdirectory that I don't like, especially for testing purposes. Are there any other better ways that I don't know about?

+3
source share
1 answer

you can use

<link href="<%= Url.Content("~/Content/somecontent") %>" />

. ( ) - , URL-. URL- .

public static class ExtensionsOfUrlHelper
{
    // TODO: Prepare for .NET 4.0, this will return MvcHtmlString
    public static string Asset(this UrlHelper url, string asset)
    {
        var path = "~/assets/" + asset;
        return string.Format("<link href=\"{0}\" />", url.Content(path));
    }
}

, ...

<%= Url.Asset("some_asset") %>

... .

.NET 4.0 , , MvcHtmlString. . ( , HTML.)

+8

Source: https://habr.com/ru/post/1725388/


All Articles