I created a sample MVC application using following Template.
ASP.NET MVC2 Empty Web Application
Then a added a Controller with the name of First and a right clicked the ActionResult to add a View.
I typed http://localhost:49565/First in my Browser.
Query
How is the controller internally getting to know that a specific page will be displayed when we will type http://localhost:49565/First ?
Moreover, If I add multiple Views for a Controller. How will the system decide which one will be displayed on Priority ?
The controller is invoked by the MVC framework, which uses the routes defined in Global.asax.cs to determine which controller and action to invoke. There is a default route that looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
When the application receives a request is will try to parse the URL to the format of the routes. If the request is made to http://localhost:49565/, it will use the default values which goes to the Index action in the controller named HomeController. When you have created the new controller, FirstController, and call http://localhost:49565/First, it uses the FirstController instead of the HomeController since it has been provided (but still to the Index action).
Further, when an action is being invoked and there is no view defined explicitly, it will look for a view named the same as the invoked action. In your case it would be ~/Views/First/Index.aspx.
EDIT
If you want to use another view you can specify it in the return statement
return View("OtherView");
and it will use ~/Views/First/OtherView.aspx instead.
Have a look at this blog posts give u the idea of how it is done
Related
In MVC4, using
Html.BeginForm("/", null, FormMethod.Post, new{something="demo"})
the resulting form has
action="//"
Because it is apparently trying to squeeze the controller and action into the url. However, I'm using the HomeController as the default controller, so I don't want /Home/action in the url. This has been working fine in most places, but this form seems to be tripping me up
Any ideas?
That's not the controller name. You would do something like this:
Html.BeginForm("Index", "Home", FormMethod.Post, new {something="demo"})
If your routes are setup correctly and Home/Index is your default root route, then when rendered, it will render to "/". MVC will automatically optimize the route to show the default minimum (which is the lone "/")
If your not seeing "/" then look at your routes, as it's not able to reverse generate the URL correctly (probably more than one route matches).
If you are using Home/SomethingElse and it's not the default route, then I don't quite get what you're trying to do, since you would be required to specify the Controller/Action to post to it.
While I don't recommend it, you should also be able to do this:
Html.BeginForm(null, null, FormMethod.Post, new {action="/", something="demo"})
I would consider this a hack, however, and I would encourage you to fix the actual route problem.
In my mvc2 project, some URL are shown in the browser like this,
localhost:53289/Paper/ViewAgendaPaper?MeetingId=186&type=2&RefId=186
but i prefer to look it like this,
localhost:53289/Paper/ViewAgendaPaper
In my Global.asax,
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Default", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
How to hide the id fields in the URL. Please give me some idea. Thankyou.
As far as I think you can't hide the Ids as they are supposed to be passed to fetch the exact data, however instead of
localhost:53289/Paper/ViewAgendaPaper?MeetingId=186&type=2&RefId=186
you can have it like:
localhost:53289/Paper/ViewAgendaPaper/186/2/186
Going through this post will help you more:
How can I create a friendly URL in ASP.NET MVC?
You cannot do what you are asking for with a simple link. An anchor link <a>...</a> performs a simple GET. If you want to hide the parameters, you would need to do a POST to the page in question.
I have created a partial view and I am using it in admin controller's Home action method. Inside partial view, I have an Account Controller with action method named Logon. I am trying to access main view's URL like this
if (Url.ToString().ToLower().Contains("/admin"))
return Redirect("/Admin/Index");
but I cant get the required result. How to get it admin/home ?
From the comments above it looks like you want to just access the current url. If so, then you can simply use the Request object. E.g.
Request.Url.AbsoluteUri
I wonder why you need this though... what is it you are trying to do exactly?
I am working on a ASP.NET application that uses ASP.NET MVC.
I tried naming one of my controllers "AdminController" meaning I typed "Admin" in the new controller text box and it filled out the controller part all by itself of course.
This controller never worked until I changed it's name. If I changed the name to anything else it worked with no problems.
I looked inside my Global.asax.cs file where the routes were configured and I found no routes leading to it.
I tryed adding a route to this new controller like this:
routes.MapRoute("Admin", "calcul/SomeAction",
new { controller = "Admin", action = "SomeAction" });
and it worked but then mysite\admin would only get routed to that specific action.
I renamed the controller to AdminSection and it works but I don't understand why it didn't work before.
Does anyone have any idea
You shouldn't need to create an explicit route for your controller if the default route matches it (controller/action/id).
Also, in the new controller text box you would need to type AdminController rather than just Admin.
The MVC framework will look for classes that end with this when looking for possible controllers.
It was me, there was a area named admin that I didn't see. When I excluded it from the project the "adminController" controller started to work
I'm creating an ASP.NET MVC 2 (RTM) project that uses areas. The Index action of the Home controller of one area needs to use RenderAction to generate a sub-section of the page. The action called is also defined in the same Home controller. So the call should just be:
<% Html.RenderAction("List") %>
However, I get an exception:
A public action method 'List' was not found on controller 'RareBridge.Web.Areas.Events.Controllers.HomeController'.
Note that I'm not in the "Events" area! I'm in a completely different area. If I remove the "Events" home controller, then the exception still occurs but names a different controller (still not the one I want it to call).
I've also tried providing the controller name and area to the RenderAction method, but the same exception occurs. What is going on here?
BTW: I am using Autofac as my IoC container
Probably action you call has filter attribute (i.e. AcceptVerbs) which doesn't match current request. Remove filters from "List" action and try again.
Use the renderaction overload which takes routeValues as parameter and use the area property to redirect to a specific area:
f.i.
<% Html.RenderAction("Edit", module.Value, new { area = "Modules", id = module.Key }); %>