WebHelpers.Mvc5 1.3.0

A collection of helpers, like sharing enums with JavaScript and version query parameters, for ASP.NET MVC5.

There is a newer version of this package available.
See the version list below for details.
Install-Package WebHelpers.Mvc5 -Version 1.3.0
dotnet add package WebHelpers.Mvc5 --version 1.3.0
<PackageReference Include="WebHelpers.Mvc5" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WebHelpers.Mvc5 --version 1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

WebHelpers.Mvc5

Build Status
NuGet

A collection of helpers for ASP.NET MVC5.

CssRewriteUrlTransformAbsolute

Converts any URLs in the input to absolute using the application's base directory.
The standard CssRewriteUrlTransform class doesn't use the application's absolute path required
by many assets.

For example, url(../fonts/glyphicons.woff) is rewritten as
url(Contoso/Content/fonts/glyphicons.woff) for an application whose base directory is Contoso.

.Include("~/Content/css/bootstrap.min.css", new CssRewriteUrlTransformAbsolute())

IsLinkActive

When building static navigation, there are two approaches to highlight the link of the current page
as active. Either you can run some JavaScript to sniff out the URLs or you can build out an if statement
for every link to determine whether or not to apply the class.

To make the second option easier, you can turn this:

<li class="@(ViewContext.RouteData.Values["Action"].ToString() == nameof(HomeController.Index) ? "active" : "")">
    <a href="@Url.Action("Index", "Home")"><i class="fa fa-link"></i> <span>Home</span></a>
</li>

into this:

<li class="@Url.IsLinkActive("Index", "Home")">
    <a href="@Url.Action("Index", "Home")"><i class="fa fa-link"></i> <span>Home</span></a>
</li>

AddVersion

Adds a cache-busting version number, which is the number of ticks since the last write time of the file,
as a query parameter to the URL of the asset.

<img src="@Url.Content("~/Content/img/user.png").AddVersion()" />

outputs

/Content/img/user.png?v=636296810982047488

EnumHandler

Renders Enums as a frozen object in JavaScript to promote re-usability between the server and client.

Add to your Web.config:

<system.webServer>
    <handlers>
        <remove name="WebHelpers" />
        <add name="WebHelpers" verb="GET" path="WebHelpers.axd" type="WebHelpers.Mvc5.Enum.EnumHandler" preCondition="integratedMode" />
    </handlers>
</system.webServer>

Add to your _Layout.cshtml:

<script src="@EnumHandler.HandlerUrl"></script>

Then decorate your enum with the ExposeInJavaScript attribute:

[ExposeInJavaScript]
public enum MyEnum
{
    Test
}

Alternatively, you can specify the enums to include and exclude via configuration.
This is helpful if you choose to keep your enums clean or if they reside in other
libraries that can't take on this dependency. To do this, you can register them in
your Global.asax.

protected void Application_Start()
{
    EnumHandler.EnumsToExpose.Include(typeof(MyEnum));
}

WebHelpers.Mvc5

Build Status
NuGet

A collection of helpers for ASP.NET MVC5.

CssRewriteUrlTransformAbsolute

Converts any URLs in the input to absolute using the application's base directory.
The standard CssRewriteUrlTransform class doesn't use the application's absolute path required
by many assets.

For example, url(../fonts/glyphicons.woff) is rewritten as
url(Contoso/Content/fonts/glyphicons.woff) for an application whose base directory is Contoso.

.Include("~/Content/css/bootstrap.min.css", new CssRewriteUrlTransformAbsolute())

IsLinkActive

When building static navigation, there are two approaches to highlight the link of the current page
as active. Either you can run some JavaScript to sniff out the URLs or you can build out an if statement
for every link to determine whether or not to apply the class.

To make the second option easier, you can turn this:

<li class="@(ViewContext.RouteData.Values["Action"].ToString() == nameof(HomeController.Index) ? "active" : "")">
    <a href="@Url.Action("Index", "Home")"><i class="fa fa-link"></i> <span>Home</span></a>
</li>

into this:

<li class="@Url.IsLinkActive("Index", "Home")">
    <a href="@Url.Action("Index", "Home")"><i class="fa fa-link"></i> <span>Home</span></a>
</li>

AddVersion

Adds a cache-busting version number, which is the number of ticks since the last write time of the file,
as a query parameter to the URL of the asset.

<img src="@Url.Content("~/Content/img/user.png").AddVersion()" />

outputs

/Content/img/user.png?v=636296810982047488

EnumHandler

Renders Enums as a frozen object in JavaScript to promote re-usability between the server and client.

Add to your Web.config:

<system.webServer>
    <handlers>
        <remove name="WebHelpers" />
        <add name="WebHelpers" verb="GET" path="WebHelpers.axd" type="WebHelpers.Mvc5.Enum.EnumHandler" preCondition="integratedMode" />
    </handlers>
</system.webServer>

Add to your _Layout.cshtml:

<script src="@EnumHandler.HandlerUrl"></script>

Then decorate your enum with the ExposeInJavaScript attribute:

[ExposeInJavaScript]
public enum MyEnum
{
    Test
}

Alternatively, you can specify the enums to include and exclude via configuration.
This is helpful if you choose to keep your enums clean or if they reside in other
libraries that can't take on this dependency. To do this, you can register them in
your Global.asax.

protected void Application_Start()
{
    EnumHandler.EnumsToExpose.Include(typeof(MyEnum));
}

GitHub repositories (0)

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.1.1 2,086 10/15/2019
2.1.0 16,181 8/17/2018
2.0.0 5,483 8/15/2018
1.3.0 1,929 5/4/2018
1.2.0 12,969 11/9/2017
1.1.0 392 11/9/2017
1.0.0 4,087 7/23/2017