Remove home directory from the URL with ASP.NET MVC - asp.net-mvc

Situation
I'll build my URL like follow:
The overview page on / (at HomeController).
The detail page on /details/123 instead of /home/details/123 knowing that 123 always is a number (at HomeController).
An info page on /info instead of /home/info (at HomeController).
The logon page on /account/signin (at AccountController).
I've don't have more pages on my application.
Try 1
I've created two routes like follow:
routes.MapRoute(
name: "Home",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem is that the account page gives a 404. The other pages works.
Try 2
I've created one route like below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
On each method inside the HomeController added a the Route attribute with the correct URL.
The problem is now that the overview and logon pages works the others gives now a 404.
Question
How could I remove the home directory from the URL with ASP.NET MVC?

To use attribute routes you need to add
routes.MapMvcAttributeRoutes();
to your RouteConfig.cs file.
It should go before your default route.

Related

ASP.NET MVC route to page with or without a route data

I'm trying to develop a 2 language site and I have the requirement that if the language route value is not specified, it must go to the page with the default language.
Example:
www.mysite.com/home/products?query=phone
www.mysite.com/en/home/products?query=phone
Both routes should respond with the result page in english.
I have this 2 routes in the RegisterRoute():
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default2",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When i browse for the second route (www.mysite.com/en/home/products?query=phone) I get a 404 Error.
Where am I wrong?
Thanks
Your url www.mysite.com/en/home/products?query=phone is not working because website engine counts en as a controller, home as action and product s as id.
If you make some research here or using google you will find a lot of solutions of your problem, but each of them has tonns of code to analyze route values, create a new url and redirect. So if your website is quite big with hundreds controllers you can wrote some code. Otherwise if you have only several controllers this coud be the easiest way
[Route("fr/[controller]/[action]")]
[Route("en/[controller]/[action]")]
[Route("[controller]/[action]")]
public class HomeController : Controller
Instead of fr you have to use the letters of your another language. And you don't need to use it for all controllers, it needs only for controllers where you can use an url that doesn't contain any language
And leave only one default route in your config and add MapMvcAttributeRoutes after IgnoreRoutes
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "default",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Correctly rendering ActionLinks on MVC site with vanityURL

I need to create site where URL patterns allow for a personalized URL like www.mysite.com brings up the main portal.
www.mysite.com/customer1 brings the portal customized for that customer (same actions and controllers called the argument for customization will be pulled from URL).
I want the prefix '/customer1/' to be maintained in the URL all the time on all pages.
I have the following routes which work fine:
routes.MapRoute(
name: "CompanyUrl",
url: "{companyurl}/{controller}/{action}/{id}",
defaults: new { companyurl = UrlParameter.Optional, controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, when I enter the site on the vanityURL so on www.mysite.com/customer1 all action links are rendered OK as
Contact
but when I enter just www.mysite.com, all links are rendered as:
Testimonials
and I cannot navigate anywhere.
How can I get them to render correctly without the companyurl parameter and without the additional /?
Assuming I understood correctly, try this (though check if it works as expected throughout):
remove the default for {companyurl} - this makes it "mandatory" for that route (meaning it will not "match" your CompanyUrl route config if not present and fall through next config which is the Default config).
So your "CompanyUrl" Route:
routes.MapRoute(
name: "CompanyUrl",
url: "{companyurl}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Hth...

Set index Home page instead of Azures This mobile app is up and running

all my redirections in Route.Config is working fine, instead of my home default link is displaying Azure's This mobile app is up and running
http://localhost:51540/
Bellow is my RouteConfig code
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
routes.MapRoute(
name: "/Index",
url: "Index/{id}",
defaults: new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
);
When I add my code without /index i get 404 on > http://localhost:51540/ and on > http://localhost:51540/Index.
routes.MapRoute(
name: "without /index",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
);
Does anybody know where can I change this configurations/settings?
Regards!
There is the .UseDefaultConfiguration() method invoked in the Startup.MobileApp.cs. The part of that is the AddMobileAppHomeController() which depends on the corresponding Microsoft.Azure.Mobile.Server.Controllers.HomeController reference. I believe it is what blocks you from the implementation of what you are doing.
Helpful post and announcement about that

ASP.NET MVC routing to action

I'm having trouble doing what I want to achieve when doing routing in ASP.NET MVC.
What I want to do is the following:
When typing http://localhost/MyWebsite/, I want to redirect to http://localhost/MyWebsite/Login/ (The redirection to the action Index is implied here)
When typing http://localhost/MyWebsite/MyAction, I want to redirect to http://localhost/MyWebsite/Login/MyAction
Point 1 was achieved using the following lines in the file RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
But I cannot accomplish point 2. Note that the user does not come from another action in a controller, but actually types the address in his browser.
Thanks for your help.
since /MyWebsite/Login and /MyWebsite/MyAction both have two segments in the URL, they're both matching your defined Route.
You can use a Route Constraint to only match /MyWebsite/Login to your first point, followed by a modified second route mapping:
routes.MapRoute(
name: "Default",
url: "MyWebsite/Login/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Actions",
url: "MyWebsite/{action}/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });

ASP.NET MVC 4 Core routing

I have set up the following structure for my website. (see link below)
http://postimg.org/image/ut4klihrp/
localhost:62540 Goes to the index page of the core
localhost:62540/www/Home Goes to the index page of the WWW
localhost:62540/cms/Home Goes to the indec page of the cms
I basically want the 'default' route (localhost:62540) to go to my WWW project. How can i do this or does anybody know a tutorial were the principle for this get explained? Or is this not possible since i use the area method.
Eventually i want to remove the view and controller from the core project.
Route config in www :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);
You can specify the default area in the defaults:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "www", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "WWW.Controllers" }
);

Resources