I have three areas: tpl1 - tpl2 - tpl3
And when I start the application I choose which area to use:
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" },
new[] { "LojaOnline.Controllers" }
).DataTokens.Add("area", "tpl1");
(The "tpl1" in DataTokens.Add is dynamic.)
The Url is: http://localhost/tpl1/Home/Index
I need to hide the name of the area in the URL. Something like that:
The Url is: http://localhost/Home/Index
I have this in tpl1AreaRegistration.cs
context.MapRoute(
"tpl1_default",
"tpl1/{controller}/{action}/{id}",
new {controller="Home", action = "Index", id = UrlParameter.Optional }
);
But if I remove the tpl1, the application doesn't know which controller use.
context.MapRoute(
"tpl1_default",
"{controller}/{action}/{id}",
new {controller="Home", action = "Index", id = UrlParameter.Optional }
);
As far as I know in IIS its not possible.
When you give the URL to be http://localhost/Home/Index.
First it will connect to your local IIS and inside which it will search for the Application Name as "Home" and inside it will search for the scripting page or webforms as per the Web application which you choose. But actually as per your URL you have specified your application to be tpl1.
So you cannot use http://localhost/Home/Index instead of http://localhost/tpl1/Home/Index
But yes you can hide the Index from the URL at the end like http://localhost/tpl1/Home.
Using Route file you can only control the URL's inside the application.
Related
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.
I am trying to implement a language chooser which is visible on all pages.
My application currently has two routes:
routes.MapRoute(
name: "EventDriven",
url: "{language}/{eventid}/{controller}/{action}/{id}",
defaults: new { language = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And in my shared _layout.cshtml file I have the following action links:
#Html.ActionLink("English", ViewContext.RouteData.Values["action"].ToString(), new {language="en"})
#Html.ActionLink("Français", ViewContext.RouteData.Values["action"].ToString(), new {language="fr"})
The problem I am encountering is I want the {eventid} route segment preserved, but it's not applicable on every url.
On the home index page http://localhost/MySite/, the two action links are as follows:
English: http://localhost/MySite/
French: http://localhost/MySite/fr
Which is good, but on my interior page http://localhost/MySite/en/2/Donation the action links are:
English: http://localhost/MySite/en/2/Donation
French: http://localhost/MySite/fr/Donation
If I go to http://localhost/MySite/fr/2/Donation then the action links are:
English: http://localhost/MySite/en/Donation
French: http://localhost/MySite/fr/2/Donation
The problem is the change language action link does not contain the eventid 2 information.
How do I make it so both links contain the event and language information (and any other route parameters unforeseeable in the future) without having to program explicitly for them?
What you might end up using is Html.RouteLink()
In this case I'd call it like this (changed to an array if multiple languages are required):
#{
var enRoute = new RouteValueDictionary(ViewContext.RouteData.Values);
enRoute["language"] = "en";
....
}
(remember the new RouteValueDictionary(), you don't want to overwrite the existing one)
and then:
#Html.RouteLink("English", enRoute)
It's a bit nasty and you can't get around it without using the view variable (which I dislike) but you get your whole route for the link.
A inline way to use this method:
#Html.ActionLink("English", this.ViewContext.RouteData.Values["controller"].ToString(), new RouteValueDictionary(ViewContext.RouteData.Values) {["language"] = "en"})
I've got the following routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,"Conference/{shortName}/Submission/{submission}/{action}", new { controller = "Conference", action = "Show" });
routes.MapRoute(null,"Conference/{shortName}/{action}",new { controller = "Conference", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The following (hopefully obvious) links which are all working:
/Conference/testconf
/Conference/testconf/ShowSubmissions
/Conference/testconf/Submission/firstSub
/Conference/testconf/Submission/firstSub/EditSubmission
When I'm now in Submission/firstSub and create a ActionLink like this
#Html.ActionLink("Show Submissions", "ShowSubmissions", "Conference", new { shortName = Model.confereceShortName },null)
it creates the following Link
/Conference/testconf/Submission/firstSub/ShowSubmissions
How can i let the actionlink forget about Submission/firstSub without hardcoding it there?
Where do you have a placeholder for {controller}?
The default route should look like the following sample.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{*id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
Also try to remove the /Submission/ part in your first route.
Links for posts on custom route creation and ordering:
1) Creating Custom Routes (C#)
2) Custom routing for ASP .NET MVC
3) official source from asp.net mvc
Sometimes searching for 30min isn't enough, you gotta search 2h...
ASP.NET MVC 2 RC2 Routing - How to clear low-level values when using ActionLink to refer to a higher level?
Routlink or delete the values in the constraints.
For me Routelink does the job.
Although Thanks
ElYusubov & Aleksey
Currently I'am working on an MVC project in which I try to get a kind of dynamic routing working. My idea would be that i left the original route in the global.asax.cs, so this one will take care of every controller I make. For example the Contact and Account controllers.
Above controllers will have url's like
/Contact/
/Account/Logoff/ etc.
The second route I want to add is the one that is a kind of default route when there are no controllers found. In that case I assume this will be a route to a page or pagedetails.
Url's for example will be :
/BBQ/
/BBQ/Accesoires/
I have three routes added in the global.asax.cs which I think are correct. (Also in the correct order). Below I have added the routes:
routes.MapRoute(
"DefaultRoute", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Page", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"DefaultPageRoute",
"{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });
routes.MapRoute(
"SecondLevelPageRoute",
"{category}/{subCategory}",
new { controller = "Page", action = "PageDetails", category = UrlParameter.Optional, subCategory = UrlParameter.Optional });
with this setup the calls to the controllers work fine, but to the pages like /BBQ/ it gives below error:
Server Error in '/' Application.
The resource cannot be found.
If I comment the first route and go the the /BBQ/ url it works like a charm. What am I overseeing in this routetable?
You put the default route first, so it is trying to go to a route defined by {controller = "BBQ", Action = "Index" }
That route should be the very last route. However, you need more detail in your routes. Just having a category route will cause problems.
For example, if this route is first
routes.MapRoute(
"DefaultPageRoute",
"{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });
Then a call to the URL /Contact/ will assume that you want to go to Page/Index/Contact not /Contact/Index/{id}. I would use a more specific route that signifies that you are browsing a category like:
routes.MapRoute(
"DefaultPageRoute",
"Category/{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });
So you will need to use a url www.mysite.com/Category/BBQ to view what you want, but I don't think that's all bad.
Ok, so i'm new to asp.net mvc and i'm trying to make a web application photo gallery. I've posted once on here about this issue i am having of trying to generate thumbnails on-the-fly on the page instead of the actual full-size images. Basically, the functionality i am looking for is to be able to have thumbnails on the page and then be able to click the images to see the full-size version. I am pulling the images and images info from an XML file. So, i did this so i could display them dynamically and so it would be easier to make changes later. Later, i am going to add functionality to upload new images to specific galleries (when i figure out how to do that as well). I am providing a link to download the project i am working on so you can see the code. I would appreciate any help with this! Thanks!
URL to project: http://www.diminished4th.com/TestArtist.zip
Ryan
In your global.asax.cs file, you have defined the Default route before your Thumbs route so the Galleries part of the url is mapped to a non-existent Galleries controller instead of the Gallery controller (as specified in your second route):
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Thumbs", // Route name
"Galleries/getThumb/{image}", // URL with parameters
new { controller = "Gallery", action = "getThumb", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "TestArtist.Controllers" }
);
Simply define the Thumbs route before the Default route and you should be all good:
routes.MapRoute(
"Thumbs", // Route name
"Galleries/getThumb/{image}", // URL with parameters
new { controller = "Gallery", action = "getThumb", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "TestArtist.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);