There is a naming convention to use underscore prefix for views which are not accessed directly (partial view, layouts, etc.) in ASP.NET MVC razor. I am not sure whether I should to use this prefix for view which is accessed only from child action.
No, you should not. As you said, prefixes are used by convention for views not accessed directly. But if child action has view, it means it IS accessed, so prefixing that view name with underscore would mean breaking another convention (more robust and conventional one :)) - naming views according to action names. So don't prefix them, or you will have to explicitly specify view name in child action.
Related
I am currently working with mvc4 and have a question around best practice.
I am passing back to my view, a number of links based on product information eg. product/1234 etc.
What is best practice, create the link using the routing engine in the controller and return the url as a property on the model object OR return the information to the view and generate the link there? I use automapper to map my DTO objects to model object, also considering creating the links during mapping.
What is the best practice with this?
You always create the link on the view.
The HTML helpers in the view can be used to ensure the link conforms to your routing rules.
You can see this in action in the many official ASP.NET MVC 4 Tutorials.
Why not in the controller or model?
The HTML helpers in the view are designed to create not just the URL, but also to wrap the URL in a fully formed anchor tag, etc. It isn't appropriate to have HTML in your model or controller as they shouldn't care how the data is displayed.
For example, the Html.ActionLink helper returns an a element.
I have 2 controllers which are SearchController and SearchByStaffController respectively. They are very similar and both have an action with action name "Search". When I call View("Search") in their common super class, the confusion comes. Only the "Search" view with SearchController is rendered.
Does the MVC framework get only the first view that matches the name and ignore the rest?
I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.
Thanks!
Does the MVC framework get only the first view that matches the name and ignore the rest?
Yes. The routing rules are aparsed (top to bottom) and when a rule is matched all end.
I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.
You can but I don't like that because MVC is based on conventions. So, I see forcing the path of the view a way to broke a convention. Are you sure you can't simply create two routing rules for the two methods? So you can do something like this:
return RedirectToAction("Search", "Controller1");
and
return RedirectToAction("Search", "Controller2");
user932390,
mvc uses convention over configuration. this means that the 'search' view will have to be located in both the:
views/Search
and
views/SearchByStaff
folders respectively. the only way around this is to locate the search view under the views/shared folder, then the viewengine will find it there in both cases and use it (assuming they have the same model).
Given URI /admin/article/index, why would this url mapping not work?
"/admin/$controller/$action?/$id?"{
view = "/admin/index" // no dice, ignored
//action = "foo" // uncommented, this is picked up
}
I'd like for all admin controllers to use the admin view by default (and not have to render the view in each action of each controller). Same goes for "/account/$controller/..." and any other site module that should use a common view.
Perhaps there's another way to achieve this, but assumed that UrlMappings is the place to do it...
Looks like you are trying to do something very different than what you wrote.
You already have the action mapped in the base URL mapping, and the view is automatically selected based on the controller, so you need to define different mappings for those views that don't have a controller, and yet another mapping for items with a default action of foo. The default action on controllers is index, though, so there is usually no need to supply a default action without also specifying a controller.
I think you are, in general, misunderstanding how an MVC framework works. The controller should not be rendering anything, and the views should be specific to the controller/action. If multiple controllers are rendering the exact same view, I'd be willing to bet that either the controller is rendering HTML or the view is overly complicated.
You should look into Layouts with SiteMesh, which allows you to create default template structures, then just have the specific content change through views.
As I am learning MVC 3 it has become apparent that naming is critical to getting the Dbase-Model-Controller-Views to talk together.
Is there an already existing list of naming conventions that should be used in a MVC application?
Something like:
Table names (plural)
Model names (singular of Table Name)
View folder must be same name as controller class (ie Contract -> derived from ContractController)
The only major convention is that controller class names must end with "Controller". Any other conventions simply represent best practices and are not required for your application to work.
The table and model naming conventions you mentioned are there because they make the code "read" better (Select * From products where category = 1; Products.Insert(new Product()))
MVC automatically looks for a view that matches the name of the action method and it starts looking in a folder that has the same name as the controller. However you can easily trump that by specifying the view name in your view result (return View("MyCustomName"))
Not many name conventions exist that will break your application, other than the few such having your controllers end with "Controller" and name inference of the "View()" call from the action name.
MVC is based on the ideology of convention over configuration, so as I found out slowly, there are many conventions that are useful to follow, I have yet to come across a list though. They are subtle and very convenient... usually.
This is a basic question about the routing machinery. From a new MVC project, I have this in the HomeController:
public ActionResult MyPage()
{
return View();
}
In the Views\Home folder, I have MyPage.aspx. The routing is still the default of {controller}/{action}/{id}. When I type in http://localhost:1790/Home/MyPage, it is correclty routes to MyPage.aspx. Since I haven't said anything about which view to render, how does ASP.NET MVC know to correctly route to MyPage.aspx? Looks as though the ActionResult name can also be used as the View/aspx page name...unless there is something I misunderstand in how the routing works. I can see how I end up in the Home folder, since the controller name corresponds to the View sub folder name. But does the Action name also correspond to the aspx name?
Would that work if the page was a PHP?
ASP.NET MVC subscribes to what is known as the Convention over Configuration paradigm whereas if you follow their conventions, basic things such as routing concerns will happen for you. But they also allow you to configure them if desired.
MVC implicitly assumes that if you return just View(), that you want View("MyPage") (i.e. the action name). No sense in repeating yourself unnecessarily.
It won't find a PHP file by default, but I'm sure you could override that behavior if you really wanted to. I can't imagine a sane scenario where you would be mixing PHP and ASP.NET MVC, but who knows :)
Action name is the same as the view / partial view name.
asp.net mvc doesn't work with php as far as I'm aware.
As has already been stated, ASP.NET MVC uses convention over configuration. Out of the box, your folder structure is something like this (only showing relevant portions and doing it from memory so...)
Site Root
+ Controllers
HomeController.cs
AccountController.cs
+ Views
+ Home
Index.aspx
+ Account
Index.aspx
+ Shared
The default routing handler is something similar to the following:
"{controller}/{action}/{id}"
There are default values for the route, but if you have a url that is a/b/c, it will look for action a on controller aController and pass it c as a parameter if said method on the controller accepts parameters.
A couple of things about that then need to be clarified. Again, convention over configuration:
1) All controller classes must end with Controller if you're using the default engine. That way, when a request comes in and the {controller} value is parsed, the engine adds Controller to it, looks in the Controller folder (and, thus, namespace), and locates the class.
2) By default -- this can be changed -- all views for a controller must reside in the Views/{controller} folder or in the Views/Shared folder.
3) Public methods on a controller are, by default, actions. You can hide this with an attribute to make themethod unavailable to the engine, but by default they are public.
So, when a request comes in the route is compared against all known routes (global.asax) and the first route that matches the request is accepted. The route is then parsed into the component parts to determine the controller, action, and parameters for the action.
Once the controller is identified, the engine instantiates an instance of that controller and executes the matching method (action) should it be found.
The action will return an ActionResult. View is an extensino method that actually returns ViewResult (if I remember that correctly). The default view for an action is a view of the same name as teh action residing in the Views/{ControllerName} folder.
Routing is a beast unto itself and I'd recommend a good bit of reading on it if you're going to play with it. Minutes to understand but a lifetime to master sorta thing.
To my knowledge, BTW, there is no engine that will use a php page as a view for a controller action.