Change MVC Routes - c#

I have hosted a ASP.NET MVC website on IIS 7.5. The problem is that the site name and controller name are same, due to this I have to enter the controller name twice.
I am not allowed to change the name of the site or controller. My current URL for eg.
local/home/home/action
but I have shared as
localhost/home/action
now I need to configure the application so that the application routes properly for
localhost/home/action

If you are using MVC5 you can use the Route attribute. Like so:
[Route(“yourroot”)]
public ActionResult Index() { … }
More information can be found here Attribute Routing in ASP.NET MVC 5
Hope this helps

Try to add a new route to RouteConfig.cs before others routes like:
routes.MapRoute(
name: "DefaultHome",
url: "{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
...

Related

Is it possible to host Asp.net MVC web App locally [duplicate]

In asp.net MVC the "homepage" (ie the route that displays when hitting www.foo.com) is set to Home/Index .
Where is this value stored?
How can I change the "homepage"?
Is there anything more elegant than using RedirectToRoute() in the Index action of the home controller?
I tried grepping for Home/Index in my project and couldn't find a reference, nor could I see anything in IIS (6). I looked at the default.aspx page in the root, but that didn't seem to do anything relevent.
Thanks
Look at the Default.aspx/Default.aspx.cs and the Global.asax.cs
You can set up a default route:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table.
ASP.NET Core
Routing is configured in the Configure method of the Startup class. To set the "homepage" simply add the following. This will cause users to be routed to the controller and action defined in the MapRoute method when/if they navigate to your site’s base URL, i.e., yoursite.com will route users to yoursite.com/foo/index:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=FooController}/{action=Index}/{id?}");
});
Pre-ASP.NET Core
Use the RegisterRoutes method located in either App_Start/RouteConfig.cs (MVC 3 and 4) or Global.asax.cs (MVC 1 and 2) as shown below. This will cause users to be routed to the controller and action defined in the MapRoute method if they navigate to your site’s base URL, i.e., yoursite.com will route the user to yoursite.com/foo/index:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
);
}
Step 1: Click on Global.asax File in your Solution.
Step 2: Then Go to Definition of
RouteConfig.RegisterRoutes(RouteTable.Routes);
Step 3: Change Controller Name and View Name
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
);
}
}
Attribute Routing in MVC 5
Before MVC 5 you could map URLs to specific actions and controllers by calling routes.MapRoute(...) in the RouteConfig.cs file. This is where the url for the homepage is stored (Home/Index). However if you modify the default route as shown below,
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
keep in mind that this will affect the URLs of other actions and controllers. For example, if you had a controller class named ExampleController and an action method inside of it called DoSomething, then the expected default url ExampleController/DoSomething will no longer work because the default route was changed.
A workaround for this is to not mess with the default route and create new routes in the RouteConfig.cs file for other actions and controllers like so,
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Example",
url: "hey/now",
defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional }
);
Now the DoSomething action of the ExampleController class will be mapped to the url hey/now. But this can get tedious to do for every time you want to define routes for different actions. So in MVC 5 you can now add attributes to match urls to actions like so,
public class HomeController : Controller
{
// url is now 'index/' instead of 'home/index'
[Route("index")]
public ActionResult Index()
{
return View();
}
// url is now 'create/new' instead of 'home/create'
[Route("create/new")]
public ActionResult Create()
{
return View();
}
}
check RegisterRoutes method in global.asax.cs - it's the default place for route configuration...
I tried the answer but it didn't worked for me. This is what i ended up doing:
Create a new controller DefaultController. In index action, i wrote one line redirect:
return Redirect("~/Default.aspx")
In RouteConfig.cs, change controller="Default" for the route.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
If you don't want to change the router, just go to the HomeController
and change MyNewViewHere in the index like this:
public ActionResult Index()
{
return View("MyNewViewHere");
}

MVC alternative route for custom route

i need some help.
I'm working with MVC 5, in VS 2015, and i want to configure some routes in my project.
First, i have a "Default" route, that is it:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Tipoou.Controllers" }
);
This route is used for the common actions, like:
localhost/auth/login or **localhost/sell/`
But, I have an Area, that the name is Company. And, I want to get the name company in the url, like this: localhost/companyname/{controler}/...
So, I did something like this (in the CompanyAreaRegistration.cs) :
context.MapRoute(
"Company_default",
"{company}/{controller}/{action}/{id}",
new { controller = "home", action = "Index", id = UrlParameter.Optional }
);
But, the Default route just stop working (thrown the 404 error). And, ALL the name that I put after localhost, it's calling the Company Area.
Can someone help me?
Can i do something like: try the Company route, if fail, try the Default?
Remove curly braces from company name in the template of route:
context.MapRoute("company_default",
"company/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
Also your controller class needed the following RouteAreaAttribute:
[RouteArea("company", AreaPrefix = "company")]
public class MyTestController : Controller
{
...
}

Subfolder in Controllers ASP.NET MVC [duplicate]

This question already has answers here:
Route controller that is in a sub folder
(2 answers)
Closed 7 years ago.
In my Controllers folder i want to have a subfolder called Admin.
When i go to http://localhost:port/Admin/Login/ it says the page could not be found.
RouteConfig.cs
using System.Web.Mvc;
using System.Web.Routing;
namespace ICT4Events
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
You could use the next route to handle your issue:
routes.MapRoute(
name: "AdminSubForder",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
DON'T FORGET to change default value for controller = "Home" to controller where you want to redirect when user types http://localhost:port/Admin/.
So when you go to http://localhost:port/Admin/Login/ you will use Login controller and Index action in the Admin folder.
IMPORTANT
Also put this route BEFORE default route, because if you put this code after your "Default" route ASP.NET will read your http://localhost:port/Admin/Login/ like URL with Admin controller and Login action.
Your new route "SubFolder" does not include the possibility of including an action in the route (in your case, "Admin").
Your url wants to match routie like
"SubFolder/ChildController/{action}"
If don't include the "{action}" in your route, it won't match your route. It then tries the default route, which obviously fails.
Try adding "{action}" to your route as shown in the below example
routes.MapRoute(
"SubFolder", // Route name
"SubFolder/ChildController/{action}",
new { controller = "ChildController", action = "Index" },
new[] { "Homa.Areas.Kiosk.Controllers.SubFolder" });

MVC 4 URL routing to absorb old legacy urls and forward to new domain

My domain used to point to a wordpress site where I had set up specific pages using the following format:
www.mydomain.com/product/awesome-thing
www.mydomain.com/product/another-thing
Recently I transferred my domain and now it points to an MVC version of my site. The links mentioned above are no longer valid, however the wordpress site still exists with a different domain. I'm trying to get my mvc site to absorb the previous links and forward them to
http://mydomain.wordpress.com/product/awesome-thing
http://mydomain.wordpress.com/product/another-thing
what I have right now is the following in the RouteConfig.cs
routes.MapRoute(
name: "product",
url: "product/{id}",
defaults: new { controller = "product", action = "redirect", id = UrlParameter.Optional });
and in my product controller I have the following
public void redirect(string id)
{
if (id == "awesome-thing")
{
Response.Redirect("http://mydomain.wordpress.com/product/awesome-thing ");
}
if (id == "another-thing")
{
Response.Redirect("http://mydomain.wordpress.com/product/another-thing");
}
Response.Redirect(" http://mydomain.wordpress.com/");
}
However my routing in RouteConfig.cs is not linking up correctly with my controller. I keep getting "404 The resource cannot be found" error.
I managed to solve this issue by reordering my map routes. I also changed the code in the controller and the maproute a bit, the following code ended up working.
routes.MapRoute(
name: "productAwesome",
url: "product/awesome-thing",
defaults: new { controller = "product", action = "redirectAwsome" });
routes.MapRoute(
name: "productAnother",
url: "product/another-thing",
defaults: new { controller = "product", action = "redirectAnother" });
//it's important to have the overriding routes before the default definition.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
then in the product controller I added the following:
public class productController : Controller
{
public void redirectAwsome()
{
Response.Redirect("http://mydomain.wordpress.com/product/awesome-thing ");
}
public void redirectAnother()
{
Response.Redirect("http://mydomain.wordpress.com/product/another-thing");
}
}

Setting up a simple ASP.NET MVC route

I am working with ASP.NET MVC 4 and am attempting to write a really basic route, but it's not working and getting very frustrated with it!
I want the URL http://www.mywebsite.com/my-page to trigger the controller called Page and the action method Index.
I have no other route setup apart from this:
RouteTable.Routes.MapRoute(
name: "Default",
url: "my-page",
defaults: new { controller = "Page", action = "Index" }
);
Is there something incorrect with my setup or where am I going wrong?
The error I get is:
The controller for path '/my-page' was not found or does not implement IController.
Main problem is you are trying to override the default route. In MVC4, the routes are defined in App_Start/RouteConfig.cs. The "Default" route should be the LAST route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then, for your specific route, use the following BEFORE the default route:
routes.MapRoute(
name: "MyPage",
url: "my-page",
defaults: new { controller = "Page", action = "Index" }
);
Fianlly, ensure you have a controller PageController.cs with an action Index and a View Views/Page/Index.cshtml:
public class PageController : Controller
{
public ActionResult Index()
{
return View();
}
}

Categories