3

Is there any possibility to have a custom relative path same as example at below:

In Razor:

<script src="%/scripts/theme.js" type="text/javascript"></script>

And Result:

<script src="/themes/default/scripts/theme.js" type="text/javascript"></script>

Define New PATH style same as %/ or */ or $/

Attention: I KNOW ABOUT ~/ (default relative path). I'm talking about how can I Define NEW ONE?

3
  • can you try this <script type="text/javascript" src='@Url.Content("~/scripts/theme.js")'></script> Commented Jun 11, 2017 at 9:56
  • No. Please read what I said there. I said CUSTOM this is not CUSTOM. Define new one. Of course i KNOW about ~/. something new so i be able to define same as %/ or ^/ or */ ..... Commented Jun 11, 2017 at 12:18
  • :D @hasan thank you. you didn't understand me. Commented Jun 11, 2017 at 18:21

2 Answers 2

2

Finally I found the solution. Please take a looks how Microsoft had implement it at link below: https://github.com/aspnet/Mvc/blob/1c4b0fcdf38320b2f02c0bb7c31df5bd391ace07/src/Microsoft.AspNetCore.Mvc.Razor/TagHelpers/UrlResolutionTagHelper.cs#L47

I had take a copy of this class and I renamed that to this:

[HtmlTargetElement("link", Attributes = "[href^='%/']", TagStructure = TagStructure.WithoutEndTag)]
[HtmlTargetElement("script", Attributes = "[src^='%/']")]
....
public class ThemeUrlResolutionTagHelper : TagHelper
{
   /*Implement tag helper here*/
}

And before creating trimmed string I insert my Theme URL

url = url.Remove(start, 2).Insert(start, $"~/themes/{Theme.Key}/");
var trimmedUrl = CreateTrimmedString(url, start);

And I changed the ~ to % in FindRelativeStart method

// Before doing more work, ensure that the URL we're looking at is app-relative.
if (url[start] != '%' || url[start + 1] != '/')
{
    return -1;
}

And Done!

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use ~ (tilde). There is no % operator for paths:

 <script src="~/scripts/theme.js" type="text/javascript"></script>

1 Comment

please take a looks again to example. i said CUSTOM. ~/ it's return you wwwroot and i want to have a custom one. % it was a example to have a new CUSTOM one. I want to point to the specific PATH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.