Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a MVC route ending ".js" and returning JavaScript?

I'd like to auto-generate some JavaScript files, with proper .js extensions in their URLs, using ASP.NET MVC 5.

Here's my problem;

I'm using require.js throughout my site and it's working pretty well. However, not all of my JavaScript files are real files on disk. Some of them must be generated at runtime. So I've written a controller which generates the dynamic JavaScript files, and serves them when using the default routing;

// ~/Resource/CommonRes -- from ResourceController.CommonRes()
define([], function() {
    return {
        "menuItemConfiguration":"Configuration",
        "menuItemAdmin":"Admin",
        "manageAccount":"Manage Account",
        "logOff":"Log off" 
        ...
    };
});

However, I need to have the route available as ~/Scripts/Resources/CommonRes.js -- the .js extension is vital since I'm actually returning a require.js module, to be invoked like this;

require(['Resources/CommonRes'], function(commonRes) {
    // code the uses the resource file
});

And in this case, the module named Resources/CommonRes will always be looked for at ~/Scripts/Resources/CommonRes.js. Hence the need to serve it up with that extension.

I can't seem to get the routing correct. I've tried the following but to no avail;

routes.MapRoute(
    name: "Resource Scripts",
    url: "Scripts/Resource/{action}",
    defaults: new { controller = "Resource" },
    namespaces: new string[] { "AITrackRecord.Controllers" }
);
like image 411
Steve Cooper Avatar asked Sep 22 '14 23:09

Steve Cooper


People also ask

What is MVC endpoint routing?

Endpoint routing is a feature newly introduced in ASP.NET Core that enables you to provide routing information to middleware in the request processing pipeline. Before the introduction of endpoint routing, routing resolution in ASP.NET Core MVC was performed at the end of the request processing pipeline.

What is minification MVC?

Minification. Minification is another such performance improvement technique in which it optimizes the javascript, css code by shortening the variable names, removing unnecessary white spaces, line breaks, comments, etc. This in turn reduces the file size and helps the application to load faster.

What is routing in MVC with example?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.


1 Answers

According to Darin Dimitrov,

IIS intercepts the request because it contains a file extension and hijacks it thinking it is a static file and not passing it to your application.

Add this to your Web.config:

<system.webServer>
    <handlers>
        <add name="ScriptsHandler" path="Scripts/Resource/*.js" verb="GET"
            type="System.Web.Handlers.TransferRequestHandler" />
    </handlers>
</system.webServer>

Then, the route:

routes.MapRoute(
    name: "DynamicJavascript",
    url: "Scripts/Resource/{action}.js",
    defaults: new { controller = "Resource" }
);

Sample Controller:

public class ResourceController : Controller
{
    public ActionResult MyScript()
    {
        return Content("var greeting = \"Hello World!\";");
    }
}

Result:

Result

like image 90
Rowan Freeman Avatar answered Sep 22 '22 10:09

Rowan Freeman