How to set the default page and URL in RouteConfig file - c#

This is my first time developing in MVC and have been stuck with this problem for a while now.
What I want to do
I want to make the URL look like this:
example.com/SiteName/Project/Index/3
instead of this:
example.com/SiteName/Project?projectId=3
And also, I want users to be able to type example.com/SiteName and see example.com/SiteName/Home/Index because it's the default page.
What I have
I have a page, Home Page which displays a list of projects. By clicking one of projects, Project page shows the details of the selected project.
In my RouteConfig.cs file, I have this:
routes.MapRoute(
name: "Project",
url: "{controller}/{action}/{proejctId}",
defaults: new { controller = "Project", action = "Index", projectId = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Problem
When I list the routes.MapRoute for Project first in the RouteConfig.cs file, I get the URL I want (example.com/SiteName/Project/Index/3).
However, when I try example.com/SiteName, it displays the Error.cshtml template in the Shared folder.
I found an SO question that mentioned that the order matters.
But if I switch the order, example.com/SiteName does successfully display the example.com/SiteName/Home/Index page, but when I click a project in the list, the URL changes to this: example.com/SiteName/Project?projectId=3
How can I set the default page to Home/Index and still keep the URL as example.com/SiteName/Project/3?

Change your routes to
routes.MapRoute(
name: "Project",
url: "Project/{action}/{projectId}",
defaults: new { controller = "Project", action = "Index", projectId = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Related

Disabling Default Routes on Asp.net Mvc

I am using Asp.net Mvc 5 with C#.
I want to disable default routing in my project. My map routes like;
routes.MapRoute(name: "News",
url: "haberler",
defaults: new { controller = "News", action = "Index"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When a user visits my website's news page, it's like;
www.domain.com/haberler
But also the user can visit the news page as below;
www.domain.com/news
I want to remove that "/news" or direct to seo-friendly url like; "/haberler"
So how can I disable default routing (Controller-Name-Convension) routing?
Instead of removing the "default" route, you can add a controller constraint to it
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = #"(Account|Manage|Home)" }
);
Like this, /news will return 404 Not Found.
In MVC, the convention is to map URLs to a particular Action on a particular Controller. Remove default route to disable default convention:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
www.domain.com/news will not match in any of your routes hence it will throw an error.
Try removing with this
RouteTable.Routes.Remove(RouteTable.Routes["NAME ROUTE YOU WISH TO RMOVE"]);
Hope this will helps you.

ASP.NET MVC 4 - MapRoute configuration to remove '/Home' where already exists default configuration

I have a solution, where the initial and default (RouteConfig) page is 'Login/Index'. This page, where name lets understand is used to user make login on system. After successful login, be redirected to Home page.
Login page the url showed is http://localhost:7037/ and after this, in 'Home' page is http://localhost:7037/home. How configure to controller Home in action Index dont show "home" on link? In other words, to working equal Login.
I tried some alternatives, and I still not find a solution to work in this specific case.
Thanks
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "Solution.UI.WEB.Controllers" }
);
}
Let's see if I understood the problemn
You have one Route "Default" only.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "Solution.UI.WEB.Controllers" }
);
What you have to do to "Home" disappear on Url is change the controler "Login" to "Home" on "defaults". It Will be like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "Solution.UI.WEB.Controllers" }
);
So, inside the Action "Index" on HomeController.cs you verify if the user is logged, if don't, you redirectToAction("Index","Login").
Hope that helps.
You could try via javascript replaceState on your Home View
<script>
(function () {
var path = document.URL.match(/\/\w+(?=\?)/g)[0];
window.history.replaceState('string', 'Some Title Text', path.replace('/',''));
})();
</script>

MVC Routes interference issues

I want to have the following:
Link to {controller}/{destination} and link to {controller}/{action}, for example: flights/berlin and flights/search accordingly.
My routes config is as follows:
routes.MapRoute(
name: "LandPage",
url: "{controller}/{destination}",
defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If "LandPage" is first, the route will go always to the land page with the url parameter (i.e. --> flights/search will go to flights/index with parameter destination = search) and its bad for me.
If "Default" will be first, and I try to navigate to flights/berlin, it will try to navigate to the flights controller and action = berlin, of course no such action...
The only solution I can think of is using "LandPage" first, and compare the {destination} parameter with name of action and redirect to that action... I don't like that solution... anyone can think about another solution??
Thanks!
You can set fixed routs for specific actions:
routes.MapRoute(
name: "Search",
url: "Flights/Search/{search}",
defaults: new { controller = "Flights", action = "Search", search = UrlParameter.Optional }
);
and
routes.MapRoute(
name: "LandPage",
url: "Flights/{destination}",
defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);
before your default route.

Rewriting url in asp.net-mvc4 (razor) with extention

Default of asp.net-mvc4 is http://domainname.com/products/1 with routes
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
and I want to rewrite to http://domainname.com/products/1.html that has .html extention .
Any ideas for this?
Do you mean this:
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}.html",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
Why would you want to put .html extension to a query string or route parameter?
if you take a website with html files, the extension is with the page not with the parameters. Don't know if you have any specific requirement to put it always at the end of the url. but it doesn't make any sense.
routes.MapRoute(
name: "Products",
url: "{controller}/{action}.html/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
you might have problems with iis after doing the above, please take a look at this discussion as well.
ASP.NET MVC Routing - add .html extension to routes

root directory of MVC not found

I'm new to MVC4 (or MVC at all using .NET). I have my controller and views working fine but if I navigate to
http://localhost:<port>
I get a 404. If I go to "
http://localhost:<port>/MyController
everything works fine. How do I get a default controller for ROOT of the website?
Your default route is handled by "RouteConfig.cs" which is in the folder "App_Start"
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
In fact, I never use this RouteConfig anymore. I prefer using attributeRouting. It allows to define the routes via an attribute above your controller. http://attributerouting.net/ or more precisely http://attributerouting.net/#defining-routes
It depends on what your 'root controller' is called and what the name of your landing action is. if it is MyController.Index() then you would do
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional }

Categories

Resources