Unnecessary Dotnet Core MVC "Home" and "Index" URL components? - .net-core

The default routing and URL generation for Dotnet Core 2.0 MVC produces very weird URLs. I don't remember them being like this in ASP.Net MVC.
i.e. when creating creating a new site using the Dotnet Core MVC template, we see the following URLs generated:
/Home/About « I could have sworn this used to just be /About
/Manage/Index « This definitely used to be /Manage
In the first example this is the same in the current ASP.NET MVC (.NET Template) template. I don't remember seeing the "Home" in the URL. That seems weird. Why not just /About?
In the second example there's a difference between the ASP.NET MVC (.NET Template) and the Dotnet Core one. The "Index" on the end is obviously superfluous.
Anyone know what's up here? Is there a simple way to change the default route definition to clean this up?

Take a look at attribute routing which you could use to declare your routes in this fashion:
public class HomeController : Controller
{
[Route("")]
[Route("Home")]
[Route("Home/Index")]
public IActionResult Index()
{
return View();
}
[Route("Home/About")]
public IActionResult About()
{
return View();
}
[Route("Home/Contact")]
public IActionResult Contact()
{
return View();
}
}
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#attribute-routing

Related

Error 404 creating a view in mvc5

I am linking a SQL database to my MVC ASP.Net. I have created the model, context class, web config and controller.
However for some reason when I create a view by right clicking on the 'views' folder I get a 404 error when I debug. If I create my view by right clicking next to:
public ActionResult Index()
in the Controller class i don't get this error. Is there any reason for this?
From your question, it seems like you might be creating the View in the wrong folder.
When you add a view for a Controller/Action, you must create subdirectory in ~/Views that matches your controller's name.
For example,
public ProductController : Controller
{
public ActionResult Index()
{
return View();
}
}
You would add your Index.cshtml to Views/Product:
~/Views/Product/Index.cshtml
If you wanted to return another view called "Other.cshtml",
return View("Other");

ASP MVC 5 Routing

I have an ASP MVC 5 Application, and i'm using routing attributes, i have a Demo action in a Company Controller, this action is a default action for my web site :
When i run the web site everything is good and the default action is the Demo action, but in the navigation bar of my browser the url is : localhost/:54973/, what changes i have to do to get an url like : localhost/:54973/Company/Demo/isin
[HttpGet]
[Route("~/", Name = "default")]
[Route("Demo/{isin}")]
public ActionResult Demo(string isin= "isin")
{
//code
}
Try the following method.
[HttpGet]
[Route("Company/Demo/{isin}")]
public ActionResult Demo(string isin= "isin")
{
//code
}

ASP.Net WebApi: Invalid URL generated in BeginForm

I'm beginning to use WebApi and I'm having an issue with a URL being incorrectly generated
I have an ApiController like this:
public class EntriesController : ApiController
{
public HttpResponseMessage Post(Entry entry)
{
...
}
}
And I was trying to create a standard controller (i.e. not webapi) in the same project to test this api (I already tested the api with fiddler and everything is ok there).
I assumed I could use the standard HTML helpers like this:
#using (Html.BeginForm("Post", "Entries"))
However this generates the following markup:
<form action="/Entries/Post" method="post">
and I expected it to generate
<form action="/api/Entries" method="post">
What is the correct way to generate the API url from a view?
I am using the default api and controller routes.
Thanks
#using (Html.BeginForm("Post", "Entries"))
You can not put WebAPI controller and method in MVC BeginForm like above. You need to pass MVC Controller and action to the BeginForm.
You can create a WebAPI EntriesController instance in your MVC controller, then use this instance to call the WebAPI method. See below:
//MVC Controller
public class EntriesController : Controller
{
[HttpGet]
public ActionResult Entries()
{
return View();
}
[HttpPost]
public ActionResult Entries(SomeModels model)
{
if (ModelState.IsValid)
{
var api = new EntriesController(); // Create WebAPI instance Here
api.Post(model.entry);
return RedirectToAction("Index", "Home");
}
return View();
}
}
This is technically possible by doing:
#using (Html.BeginForm("Post", "api/Entries"))
Don't forget, the "Post" value in the .BeginForm() extension method doesn't mean anything to an out-of-box Web Api route setup. Only the url and HTTP action matter (and any additional values on the URL for method overloading)
You would need to use BeginRouteForm as link generation to Web API routes always depends on the route name. Also make sure to supply the route value called httproute as below.
#using (Html.BeginRouteForm("DefaultApi", new { controller="Entries", httproute="true" }))

Using Html.Action with a controller in a subfolder

I am building a webapge using asp.net mvc4. For organization, I would like to put some of the controllers inside a subfolder in the Controllers folder. For example:
Controllers
AccountController
BlahController
Dashboard (Folder)
ChickenController
BeefController
To use BeefController (which returns a partial view), it seems as though I should use:
#Html.Action("Index", "Dashboard/BeefDashboard")
However this gets me the following error:
The controller for path '/' was not found or does not implement IController.
How would I be able to use BeefController?
There no physical sub folder concepts in the ASP.NET MVC world. What you should do is to have an action method in Dashboard controller, which accepts a parameter and then return specific views according to that.
public class DashBoardController: Controller
{
public ActionMethod Index(string id)
{
if(id=="chicken")
{
return PartialView("Chicken");
}
else if(id=="beef")
{
return PartialView("beef");
}
return View("NotFound");
}
}
Now you can access those like
Dashboard/beef
Dashboard/chicken

MVC3 - All routing works locally but not remotely

I have a new MVC3 project with one Controller called PublicController.cs which contains 4 identical methods for testing out how routing works. The only difference between them is their name, and that they each point to a different view ...
public class PublicController : Controller
{
//
// GET: /Public/
public ActionResult Index()
{
return View();
}
//
// GET: /Public/App
public ActionResult App()
{
return View();
}
//
// GET: /Public/Press
public ActionResult Press()
{
return View();
}
//
// GET: /Public/Contact
public ActionResult Contact()
{
return View();
}
}
I can get to all of them when running in the development server by visiting these URLs...
http://localhost:53367/Public/
or its equivalent
http://localhost:53367/Public/Index
and then
http://localhost:53367/Public/App
http://localhost:53367/Public/Press
http://localhost:53367/Public/Contact
However, once it's deployed to my remote ASP.NET 4.0 server, the only two that work are:
http://localhost:53367/Public
http://localhost:53367/Public/Index
... all others give me a 404 Resource cannot be found error.
My web-server is shared hosting with netcetera, using a sub-domain for this deployment (previously had problems with MVC in virtual directories, but have full blown MVC2 apps running in sub-domains no problem). I've deployed by using the "Publish to file system" option, then copying over the files aswell as just copying the entire source project over. Both give identical results.
Any ideas why?
Thanks,
Steven
Did you make sure MVC 3 Framework is installed to the web server, and that your site's app pool is set to 4.x ASP.net?

Resources