Reputation: 1387
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?
Upvotes: 3
Views: 2435
Reputation: 1387
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!
Upvotes: 2
Reputation: 21810
You need to use ~
(tilde). There is no %
operator for paths:
<script src="~/scripts/theme.js" type="text/javascript"></script>
Upvotes: 1