An open API service indexing awesome lists of open source software.

https://github.com/c0shea/webhelpers.mvc5

Helpers for ASP.NET MVC5
https://github.com/c0shea/webhelpers.mvc5

asp-net-mvc5 csharp

Last synced: about 1 year ago
JSON representation

Helpers for ASP.NET MVC5

Awesome Lists containing this project

README

          

# WebHelpers.Mvc5

![Build Status](https://coshea.visualstudio.com/_apis/public/build/definitions/c4f53986-29b5-45cd-b130-b074bbc802b0/4/badge)
![NuGet](https://img.shields.io/nuget/v/WebHelpers.Mvc5.svg)

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.

```cs
.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:

```cshtml


  • Home

  • ```

    into this:

    ```cshtml


  • Home

  • ```

    ## IsTreeviewActive

    Similar to `IsLinkActive`, this makes it easy to determine whether the treeview is the active link.

    ```
    @{
    var treeviewActions = new Dictionary
    {
    { "Action", "Controller" }
    };
    }


  • Action


  • ```

    ## 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.

    ```cshtml

    ```

    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:

    ```




    ```

    Add to your _Layout.cshtml:

    ```

    ```

    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));
    }
    ```

    ## ClientIP

    Gets the IP address of the client sending the request. This method will return the originating
    IP if specified by a proxy but makes no guarantee that this is the client's true IP address.
    Since these headers can be spoofed, you are encouraged to perform additional validation if
    you are using the IP in a sensitive context.

    ```
    var ip = HttpContext.Current.Request.ClientIP();
    ```

    ## jqGrid

    ### License

    jqGrid is not licensed under the MIT license like this project. See [here](http://guriddo.net/?page_id=103334)
    for its license. It is only included in the Demo project for the purposes of demonstration
    in a non-commercial capacity.