views:

35

answers:

1

I have a control in ASP.NET MVC that spits out JavaScript in the page header (in the view page). I derive some values from the current view name (including its case). Let's say we are talking about the path: /Home/Index - my control spits out JavaScript to call a function with the view name - in its exact case - e.g. someFunction('Index'). Now when I try to navigate to my view using '/home/index', the view name is returned as 'index' which is causing issues in my JavaScript as I rely on the casing for it on the JS side.

Is there any way to know the exact view name (as it was defined) from the path that got mapped into this view.

A: 

Have you thought about using a custom ViewFactory which stores the view name somewhere you can retrieve it? Something like this:

public class MyViewFactory : WebFormViewEngine
{
    public override ViewEngineResult FindView(
        ControllerContext controllerContext, 
        string viewName,
        string masterName,
        bool useCache
        )
    {
        controllerContext.Controller.ViewData["viewname"] = viewName;
        return base.FindView(controllerContext, viewName, masterName, useCache);
    }
}
pdr