3

Here is an example of what I am trying do.

Provide an MVC Route from test.js to a controller action e.g.

> http://localhost/MVCScripts/test.js?id=123

to the controller

> http://localhost/Home/RenderJavascript?id=123

The reason behind this is to produce a dynamic js file rendered from the server based on the id. The JS file will be linked to by being embedded into other websites.

I have tried setting the web.config to exclude the script folder in question:

<handlers>
     <add name="scripts" path="/MVCScripts/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" />
</handlers>

and then added a route such as:

routes.MapRoute(
            name: "EmbedCode",
            url: "MVCScripts/test.js",
            defaults: new { controller = "Home", action = "RenderJavascript", id = UrlParameter.Optional }
        );

When invoking

> http://localhost/MVCScripts/test.js?id=123

I just get

The controller for path '/MVCScripts/test.js' was not found or does not implement IController.

Note that my aim here is to use MVC Routing. Thanks

1 Answer 1

3

If you are running in IIS integrated pipeline mode make sure that you add the following handler to the <handlers> collection of your <system.webServer> node:

<system.webServer>
    <handlers>
        ...
        <add name="DynamicScript" path="MVCScripts/test.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
   </handlers>
</system.webServer>

This will ensure that the static .js extension will be routed through the managed ASP.NET pipeline instead of being directly served by IIS as a static file (and obviously not being found in your case). And when it gets served through the ASP.NET pipeline it will obviously hit your route that you must define before the default one:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "EmbedCode",
        url: "MVCScripts/test.js",
        defaults: new { controller = "Home", action = "RenderJavascript", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.