Hide one controller name from mvc url, show other controller names - asp.net-mvc

I have two controllers, HomeController and ResourcesController.
I want to hide the Home/ from the url when action on HomeController is requested, but for ResourcesController (or any other controlelr) I want to keep the controller name in url.
E.g. /Home/Products will be /Produtcs, but /Resources/Banana should stay /Resources/Banana
These are my routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SpecificRoute",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Home", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Home", id = UrlParameter.Optional }
);
It works as expected for home controller but for resources controller I get "404... The resource cannot be found"

One possible way is you can map all the home page actions within the global.asax file. See the example code below.
e.g.
routes.MapRoute(
"ProdutcsRoute", // Route name
"Produtcs/{id}", // URL with parameters
new { controller = "Home", action = "Produtcs", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"AboutRoute", // Route name
"About/{id}", // URL with parameters
new { controller = "Home", action = "About", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Related

MVC RegisterRoutes MapRoute

routes.MapRoute(
name: "Home",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "", id = UrlParameter.Optional }
);
1. Could you please help me in understanding why I get HTTP 404 error when I hit http://localhost:7841/Process
However, I am able to see my page when I hit
http://localhost:7841/Process/list
Also, if i hardcode controller( url: "Home/{action}/{id}") in both the routes URLs(see below) why I get “HTTP Error 403.14 - Forbidden” error.
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "", id = UrlParameter.Optional }
);
Kindly help me in understanding Routes.
Because when you request yourBaseUrl/Process/, It matches the route pattern {controller}/{action}/{id} which is the url pattern for your first route defined(the one called Home). So it will try to send the request to the action method and since you do not have the action method segment in the request url, it will try to use the default one defined in that route registration, which is Index. You are getting a 404 because you do not have an Index action method inside your ProcessController. If you add an Index() action method to your ProcessController, It will execute that and return the result from that.
Ideally, you should define all your specific route definition before the generic route definition. If you want /Process to return the response returned by the List method, set that as the default action in the route registration.
routes.MapRoute(
name: "Process",
url: "Process/{action}/{id}",
defaults: new { controller = "Process", action = "List", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Another option is to use the default generic route registration as it is in the RouteConfig and use attribute routing to make List method to be handled by /Process/ request.
public class ProcessController : Controller
{
[System.Web.Mvc.Route("Process")]
public ActionResult List()
{
return Content("process list action method :)");
}
}

MVC Routing Issue when trying www.example.com/id

Let say I have a website www.example.com
the default routing looks like
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
Ok that works fine but let's say I want my site when I go to www.example.com/id to go to www.example.com/login/index/id
How would I configure/add routing for this, without breaking my other pages where I am actually trying to go to www.example.com/controller?
EDIT: Unfortunately id is a string so I do not have any concrete constraints that I can think of that would work. Think of maybe instead of the id I should have said companyname or sitename so the URL would look like www.example.com/companyname .
The only solution that I have come up with so far is adding a maproute for each one of my controllers like this
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Settings",
url: "Settings/{action}/{id}",
defaults: new { controller = "Settings", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "companyname",
url: "{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
This will work but I have many controllers and if I add one in the future and forget to adjust the routes it will fail. Also, this is unlikely but if a companyname happens to the be same as one of my controller names it would also fail.
In controller you may redirect to another Controller/action:
public ActionResult yourAction()
{
return RedirectToAction("nameAction","nameController");
}
Did you tried adding this mapping first:
routes.MapRoute( name: "Custom", url: "{id}", defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional } );
That should work but keep in mind that routes are evaluated secuentially, so you will have to organize mappings in order to reach out all pages in your site.
For example, routes like www.example.com/Product could be redirected to /Login by mistake.
EDIT: You can add constraints, so if id is an int value, you can try with the following:
routes.MapRoute("Custom", "{id}",
new { controller = "Login", action = "Index" },
new { id = #"\d+" }
EDIT 2: Having ids as string values, the only solution I see is to manually add each controller as you said, or to add something like this:
routes.MapRoute(
name: "Default",
url: "app/{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
This way you don't need to update each route in the future.
Please try below routing
routes.MapRoute(name: "companylogin", url: "companylogin/{id}", defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
routes.MapRoute(name: "default", url: "{controller}/{action}/{id}", defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
Remove other controller specific routing. Now you can navigate to login using
url : - www.example.com/companylogin/{id} and all other url redirect default route.

MVC Routes for an multi tenant application

I have a MVC application and can't quite get the routing working for my multi-tenant application. Here is the problem:
I have 2 types of pages in my application, most require the tenant name to be in the url but some don't. e.g.
These Do (tenant name is these examples is samsung and apple):
http://www.mytestapp.com/samsung/customers/add
http://www.mytestapp.com/apple/customers/add
These Don't:
http://www.mytestapp.com/home/register/
http://www.mytestapp.com/home/aboutus/
What routes do i require to get this working? I have tried this but it does not work for the register and about us page.
routes.MapRoute(
name: "TenantRoute",
url: "{tenantid}/{controller}/{action}/{id}",
defaults: new { tenantid = "tenantname", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Well, you'll need a second route to match the non-tenant routes. The default one should match if register and aboutus are controllers with an Index action:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);
try with this
routes.MapRoute(
"samsung",
"samsung/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional}
);
routes.MapRoute(
"apple",
"apple/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);

How can I make my routes that start with /home always direct to the home controller and index action?

I have the following route:
routes.MapRoute(
name: "Default",
url: "home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This works fine and directs to the home controller Index action where the user enters /home
Is there a way that I can ALWAYS make it direct to the Index action if the user enters:
/home/xxx or
/home/xxx/xxx
Where xxx can be anything?
Sure, just remove the {action} part from your route definition so that the action is always Index:
routes.MapRoute(
name: "Default",
url: "home/{anything}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you remove the {action} from the url and keep it as a default value then it should work fine.
routes.MapRoute(
name: "Default",
url: "home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In this scenario action will ALWAYS be Index...
Try this to catch every possible route:
routes.MapRoute(
"Default",
"home/{*url}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } }
);

Routing issue in Global file

Routes Information
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
routes.MapRoute(
"Default1", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Url Information
http://localhost:24060/home/22323 //Failed
http://localhost:24060/home/index/22323 //Passed\
Query, How can i pass both url ?
You have to map the default route last. Also you should create a constraint in the other route to not to block the default route.
routes.MapRoute(
"Default1",
"{controller}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { id = #"\d+" });
//second segment has to be an integer, otherwise skip this route.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });

Resources