Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get visual studio to recognize a cshtml file as javascript

I am writing a css/js page that has some dynamic parts in it. To do this i am using a cshtml file containing css/js - i am using mvc.net and returning the css from a controller action.

The trouble is visual studio recognizes this page as html and not as javascript/css so it does not give me javascript/css coloring and IntelliSense.

My questions:

  1. is there a better/easier way of creating dynamic css/js in .net
  2. How can i get visual studio to recognize a cshtml page as javascript.
like image 253
Daniel Avatar asked Nov 13 '22 20:11

Daniel


1 Answers

I know this is an old post...but...

What I did was just put the script tags around my javascript files in their own .cshtml file. I created a separate controller (JavascriptController.cs), and I created a filter on that controller that removes the script tags. I set the filter in the OnActionExecuting method. by just doing

this.Response.Filter = new ScriptFilter(Response.Filter, Response.ContentEncoding);

So you get the syntax, you get razor without having to use RazorJS, and you can request the js files like regular routes in an MVC application. You just have to keep the script tags on the partial view while editing.

So you can call

/Javascript/{Action}

and you'll get your javascript file with your razor in it, and the filter will remove the script tags so you can include it like a normal script.

<script src="http://{host}/Javascript/{action}"></script>
like image 196
Callan Avatar answered Nov 16 '22 20:11

Callan