3

New to ASP.NET MVC, I understand that, if I right-click in an action within my controller, the context menu gives me Add View, and Go To View. Where does the app store that connection? I have searched all the files in the app for the name of one of my views (cshtml files), and haven't seen anywhere that a particular one is connected to any particular controller, let alone controller method.

I know this is a newbie question, but I have searched around for an answer and haven't come up with one.

1
  • 1
    How this works is described completely here: asp.net/mvc/tutorials/older-versions/views/…. Specifically, The path to the view is inferred from the name of the controller and the name of the controller action. Commented May 21, 2013 at 21:04

1 Answer 1

5

The answer is that it doesn't store this 'connection'. MVC uses a concept known as "Convention over Configuration". This means that MVC "infers" various things based on conventions. In this case, the convention is that the view is automatically found if it's in a folder in the Views Directory with the same name as the controller, and the same name as the method.

You can override this, by passing a view name in the View() call, but otherwise it just figures it out at runtime.

The IDE also uses this convention, and it has parsed your code file and knows to "Go to" the file specified by convention.

Sign up to request clarification or add additional context in comments.

4 Comments

This "connection" is in fact coded in to the ViewEngine itself as part of the list of paths to search for the view. As you've stated, the name of the view is inferred from the name of the Controller and Action. You can change this by implementing additional paths or by making your own ViewEngine.
@NickBork - Thanks - where would I look to see this list of paths in the "ViewEngine itself"?
@NickBork - I wouldn't call that a "connection", it's still a runtime computed value based on convention, even if the search folders are hard coded. The question asker was asking where the view name was associated with the view, and that doesn't exist if you call the parameterless View() method.
@KellyCline - this is hard coded, you can look at the MVC source code on codeplex, but other than that you just have to know these paths. You can look here though aspnetmvcninja.com/views/view-search-paths

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.