Why Controller suffix removed from URL in MVC/API application - asp.net-mvc

In MVC/web API application controllers are suffix with Controller keyword.
But when any action called or rendered on browser, the URL generated in browser will remove the Controller suffix.
For example,
I have controller HomeController with action UserList.
When this action rendered in browser, the URL generated in browser will be looks like http://localhost:123/Home/UserList
So I want to know that from where Controller suffix is removed from browser's URL?

ASP.NET MVC uses Convention over Configuration. MVC identifies the right Controller and its Action method from the URL using the Route data. More details about MVC pipeline/lifecycle are here and here.
It would be good to take a look at the DefualtControllerFactory of ASP.NET Core in github.

Related

Is it possible to not using url for routing in MVC?

In a mvc project that we have developed for 6 months to now, we don't want to change the url when redirecting to another view. For example, our domain is xyz.net, we want customers to see only our domain name in url. Is it possible? Can we do it with mvc routing?
When we use return View("ViewName"), url is not changing. Is it meaningful to use for all in project for not changing url?
Now project contains RedirectToAction("ViewName", "ControllerName") for redirection between views, and it is changing url like "xyz.net/controllername/viewname".
How can we do without using url routing?

Unable to open a published MVC Project

I'm a total beginner so sorry in advance.
I used VS13 to build a MVC project and published it to my webspace. Now I'm unsure which file or path I need to specify in my forwarding config in order to open the website.
I tried
/Views/Shared
to get _Layout.cshtml and
/Views/Home
to get Index.cshtml but none of these are working. I also changed some admissions but it always shows me this
Forbidden - You don't have permission to access / on this server.
when I'm trying to open the website.
Any ideas on what I'm doing wrong?
With MVC You don't access views like traditional ASP.NET WebForms i.e. /path/to/view.aspx. Everything is handled via routing & controllers.
By default you will have a HomeController which will have an Index action which is invoked via a GET request. Assuming you haven't changed any of the default routing configuration you would just need to navigate to www.domainname.com/home to see your Index page.
The default routing configuration looks like /controller/action/parameters, MVC will always work this way unless you tell it different. If you don't pass a specific action (like I didn't with the home url) the Index action of the controller is assumed.

single page application (SPA) home page error

I am new to asp .net MVC. I am trying to implement SPA model for my new application. I am using asp .net web api in asp .net mvc4 project. I have only api controllers in my project. I dont have any MVC controllers.
I have deleted auto generated views from the views folder. I have created a index.cshtml page outside the views folder. This page is my layout page that renders other pages in it. I have set this page as startup page. And also i have commented out the default MVC route from routeconfig.cs file.
The problem is when i run the application, the index.cshtml does not render. I get an error saying "This type of page is not served - Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect."
Please help
Make sure you keep the root Views folder, and create a folder called Home within that. Place your Index.cshtml file within the Home folder.
Do not comment out the default MVC route. In MVC, you can't hit a view directly, as you do in WebForms, which is why you get the error when you try to hit Index.cshtml.
Make sure you have a HomeController within the root Controllers folder, with an action called Index that looks like this:
public ActionResult Index()
{
return View();
}
Your view should now render when you browse http://localhost/Home/Index or even just http://localhost as by default, the controller and action will be set to Home/Index.

working with an .aspx page with in asp.net mvc 2

I'm new to asp.net mvc.
I just created the default asp.net mvc project in VS and I can see that when I make a call to a controller's action like this: "http://localhost:2528/Home/About" my Home controller has an action method "About" that is returning the about.aspx view. however I am seeing not seeing the .aspx extension in the browser's url. And when I try to browse to "http://localhost:2528/Home/About.aspx" i get a 404 error.
I have a requirement where I need to create a .aspx page that is passed an arguement via the url like this: "http://... /myAspxPageHere.aspx?argName=myArg"
I'm not sure how to do this with asp.net MVC. Any help and/or code examples would be greatly appreciated.
Thanks
ASP.Net MVC does not use the .aspx extension in client-facing URLs.
If you want to accept arguments from the querystring, you should add parameters to your action methods.

Use Anchor Link in asp.net MVC

I put an html anchor link in one of my Views. Just like this:
<a href="SomePage.html"> Some Link<a>
It does not go to the page. I suppose it is because it does not have a Controller and is not wired up with MapRoute.
My question is: Can I use a regular html anchor tab in an MVC application or Must I wire it up with the MVC Controller and MapRoute?
This is because an HTML file in a view folder doesn't automatically have an action associated with it. Remember, with MVC, views are just something that is rendered by a controller that is serving a route. The views folder is just a handy place to store these views, and doesn't map to any actual URL's unless there is a route specified.
The general convention is to put these kinds of files in the Static or Content folder, where they will be served up just fine.
You should be able to. Is the path correct? What page is it on? Are you going up the path tree?

Resources