I've used this routing in localhost and it works fine:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Authentication", action = "Login", id = UrlParameter.Optional }
);
But when I deploy to a server with this structure:
servername/folder/Controller/Action
the styles and the pages get 404 error.
I want to change the routing dynamically between localhost and server
Stylesheets do not use routing they are in a set path that you set in the _layout If you want to set them dynamically you can use your web.config and do a transform to set the location based on the environment you are deploying to.
If you want to do dynamic routing then you need a placeholder, and you will need to supply the value at runtime. The files must exist in the physical path that matches the route
routes.MapRoute(
name: "Default",
url: "{servername}/{folder}/{controller}/{action}/{id}",
defaults: new { servername = "servername", folder = "foldername",
controller = "authentication", action = "Login", id = UrlParameter.Optional }
};
Related
I need to have 2 routes in my ASP.NET MVC application, and I am having difficulty trying to define the routes in a way that they don't steal from each other.
I need to handle urls like
www.website.com/Home/About
www.website.com/Home/Contact
I also need to urls like this
www.website.com/23a244x3/Products/2
www.website.com/434e5s344/Services/1
www.website.com/f2432g423/Profile
So I need 2 routes.
The urls in #1 are successfully handled by the default route, which is
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To handle the urls in #2, I have defined the following route
routes.MapRoute(
name: "PagesRoute",
url: "{uniqueID}/{action}/{pagecount}",
defaults: new { controller = "Pages", pagecount = UrlParameter.Optional }
);
Unfortunately now the route #2 (aka PagesRoute) is also handling the urls from #1, such as www.website.com/Home/About. I also have url rewrite rules set in IIS that turn all urls into lowercase, for SEO purposes, so /Home redirects to /home etc.
How can I differentiate these two routes? The route in #2 seems to be greedy..
You would have to not provide controller, and provide routes for each controller:
routes.MapRoute(
name: "Default",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To the parser {uniqueID} and {controller} are essentially the same. Any string in the Url, will match both routes, so you need something to differentiate the 2.
You can also take a look into Route Constraints:
routes.MapRoute(
"PagesRoute",
"{uniqueID}/{action}/{pagecount}",
new { controller = "Pages", pagecount = UrlParameter.Optional },
new { uniqueID = #"\d+" }
);
I have built a standard MVC application with controllers and views and the following routes:-
routes.MapRoute
(
name: "PageNumber",
url: "{controller}/{action}/page-{pageNumber}",
defaults: new { controller = "Home", action = "PageNumber" }
);
routes.MapRoute
(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
Because this a back-end system, there will be a basic HTML website on the front of this. It means I need to route my site into a subfolder, so that the URL's look like this:-
SubFolder/Controller/Action/{id}
How can I do this, without changing all of my hard-coded links to include this sub-folder. I can't use MVC Areas for this, so was wondering if there was a way of changing the routing to automatically pre-pend the SubFolder bit of the URL?
Thanks!
You could make a new route:
routes.MapRoute(
name: "Subfolder",
url: "Subfolder/{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
However you should not be hard coding links, if you have future plans to replace the basic HTML page you could use #Html.ActionLink to generate the anchor tags for you.
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 }
);
I have a couple of routes in my config that seem to work OK in the IIS express dev environment, but not in IIS. I can get to the route BlogArchive route, but not the BlogDetailroute.
routes.MapRoute(
name: "BlogDetail",
url: "Blog/{Slug}",
defaults: new { controller = "Blog", action = "Detail", slug = UrlParameter.Optional }
);
routes.MapRoute(
name: "BlogArchive",
url: "Blog",
defaults: new { controller = "Blog", action = "Index" }
);
This can be fixed by just adding an additional pointer to the BlogDetail i.e. making the whole url map to Blog/Entry/{Slug}, but I would rather not have to have that extra bit. I can't seem to find any examples on the web, so excuse me if I am missing something simple.
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 }