I personnaly don't like to play with httpHandlers for security reasons. I wanted to do the exact same thing to prevent having to maintain the same folder structure twice (in view and in scripts folder). So, the goal is to store the .js in the same folder as my .cshtml and no longer have the 404 error.
Solution
To reach that goal, I use a custom HtmlHelper and a Controller for javascript calls.
HtmlHelper
public static MvcHtmlString JScriptBlock<TModel>(
this HtmlHelper<TModel> html
)
{
// Get the physical path of the .js file we are looking for.
string path = ((System.Web.Mvc.RazorView)html.ViewContext.View).ViewPath.Replace(".cshtml", ".js");
path = HostingEnvironment.MapPath(path);
if (!File.Exists(path))
return null;
// We store the physical path in a session variable with GUID as the key
string guid = Guid.NewGuid().ToString();
HttpContext.Current.Session[guid] = path;
// Create the script block where the src points to the JScript controller. We give the GUID as parameter.
return MvcHtmlString.Create("<script src='/JScript/?id=" + guid + "'/>");
}
JScript Controller
public ActionResult Index(string id)
{
// id correspond to the guid generated by the MSRJScript helper
// We look if the physical path of the .js is available in the session variables
if(Session[id] == null)
return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
// If the physical path was found, we simply send the file back to the browser.
string path = Session[id].ToString();
Session.Remove(id);
return File(path, "application/javascript");
}
Once done, you simply have to add the following code in your View/PartialView
@Html.JScriptBlock()