views:

661

answers:

4

Instead of a *.cs code behind or beside I'd like to have a *.js file. I'm developing a MVC application an have no need for a code beside because I have controllers, but in certain cases it'd be nice to have a JavaScript code beside or some way to associate the file to the page it's being used on. I suppose I could just name them similarly, but I'm wanting to show the association if possible so there's no question about what the file is for.

Typically what I'm talking about is within Visual Studio now under your Global.asax file you will have a plus sign to the left:

+ Global.asax

Once you expand it you'll get

- Global.asax
    Global.asax.cs

I'd like the same thing to happen:

+ Home.spark

- Home.spark
    Home.spark.js

Updated:

My existing csproj file has a path to the actual file, not sure if that's screwing it up. I've currently got:

<ItemGroup>
    <Content Include="Views\User\Profile.spark.js">
      <DependentUpon>Views\User\Profile.spark</DependentUpon>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <Content Include="Views\User\Profile.spark" />
  </ItemGroup>

and it's simply just showing the files besides each other.

+6  A: 

Absolutely - but you'll have to edit the project file by hand. Find your "child" file's entry, and change it to be a non-self-closing element, and add a child element to say what it depends on. Here's an example for a .cs file being compiled:

<Compile Include="AssertCount.cs">
  <DependentUpon>MoreEnumerable.cs</DependentUpon>
</Compile>

And here's a version I've just mocked up for the files you've specified - although I expect Home.spark will have an action associated with it rather than "None":

<ItemGroup>
  <Content Include="Home.spark.js">
    <DependentUpon>Home.spark</DependentUpon>
  </Content>
</ItemGroup>
<ItemGroup>
  <None Include="Home.spark" />
</ItemGroup>

Visual Studio displays it just as you'd expect it to.

Jon Skeet
Actually, right now spark is just associated with None:<None Include="Views\User\Profile.spark" />I'll give it a shot, thanks for the answer!
rball
Tried: <ItemGroup> <Content Include="Views\User\Profile.spark.js"> <DependentUpon>Views\User\Profile.spark</DependentUpon> </Content> </ItemGroup> <ItemGroup> <None Include="Views\User\Profile.spark" /> </ItemGroup> Didn't work :(
rball
@rball - <DependentUpon>Profile.spark</...> It is assumed that they are in the same folder.
Marc Gravell
Bingo you got it! Why didn't you post it as an answer?
rball
+2  A: 

I don't know how you can do it via the Visual Studio UI itself, but if you open the csproj file in a text editor, you can see how they do it for Global.asax.

It usually looks something like this:

<Compile Include="Global.asax" />
<Compile Include="Global.asax.cs">
    <DependentUpon>Global.asax</DependentUpon>
</Compile>
Yeah, I did see that. I tried to model mine after that but it didn't work. Currently looks like:
rball
<ItemGroup> <Content Include="Views\User\Profile.spark.js"><DependentUpon>Views\User\Profile.spark</DependentUpon> </Content> </ItemGroup> <ItemGroup> <None Include="Views\User\Profile.spark" /> </ItemGroup>
rball
+5  A: 

There is VSCommands add-in which allow you to configure dependant files directly from IDE

Very cool, thanks for the addition.
rball
updated link to VSCommands: http://mokosh.co.uk/vscommands
jarek
A: 

Whilst the other solutions here (editing the project file) work for individual files, we've done the same thing, but want it to work automagically for all *.conv and *.conv.js files rather than having to edit the project file for each one.

When we add a new .conv file to our app, if we have a *.conv.js file it automatically nests under it as you'd expect with .aspx and *.aspx.cs.

I can't find the original tutorial I used to set this up (it does involved registry hacking) but here is one I found from a Google search just now that describes the same thing:

http://blog.dotnetwise.com/2009/09/visual-studio-2008-custom-nested-files.html

Michael Shimmins