Giter Club home page Giter Club logo

webhelpers.mvc5's Introduction

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>

IsTreeviewActive

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

@{
    var treeviewActions = new Dictionary<string, string>
    {
        { "Action", "Controller" }
    };
}

<li class="treeview @Url.IsTreeviewActive(treeviewActions)">
    <a href="#"><i class="fa fa-cogs"></i> <span>Action</span> <i class="fa fa-angle-left pull-right"></i></a>
    <ul class="treeview-menu">
        <li class="@Url.IsLinkActive("Action", "Controller")">
            <a href="@Url.Action("Action", "Controller")"><i class="fa fa-trash"></i> <span>Action</span></a>
        </li>
    </ul>
</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));
}

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 for its license. It is only included in the Demo project for the purposes of demonstration in a non-commercial capacity.

webhelpers.mvc5's People

Contributors

c0shea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

webhelpers.mvc5's Issues

Add IsTreeViewActive

Need a way to set the active class on both the treeview and its child element.

ReflectionTypeLoadException

The call to assembly.GetTypes() sometimes throws a ReflectionTypeLoadException when one or more of the inner types reference an assembly that isn't loaded and can't be found.

These transitive dependencies aren't necessarily required for the application to function but they throw the exception when we try to find all types that have the ExposeInJavaScript attribute.

See https://stackoverflow.com/a/7889272/4403297 for a solution to this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.