.NET MVC Route not working propely - c#

I have defined my routes in .NET app like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Register",
url: "Register",
defaults: new { controller = "Home", action = "Register" }
);
routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Home", action = "Login" }
);
routes.MapRoute(
name: "ResetPwd",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "ResetPwd", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
So that I can have routes like following:
mysite.com/Login
mysite.com/Register
mysite.com/ResetPwd
Now the problem arises with other controller when I try to redirect the user to dashboard when hes logged in successfully from mysite.com/Login like this:
return RedirectToAction("Index", "Dashboard");
However I'm getting an error:
The resource cannot be found.
I presume it want's me to define a route now for dashboard/index... but isn't that nuts since then I'd have to add every route manually ???
What am I doing wrong here?

Change your resetpassword route to
routes.MapRoute(
name: "ResetPwd",
url: "ResetPwd/{id}",
defaults: new { controller = "Home", action = "ResetPwd", id = UrlParameter.Optional }
);
Currently your provided url /Dashboard/Indexis looked up by this route with Dashboard being an Action on Default Home controller which doesn't exists in my opinion.

Related

How to use multible routes in MVC

I have a website that need a departmentID for all sites, so i rewrote the default route to the following:
routes.MapRoute(
name: "Main",
url: "{deptID}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", deptID = "", id = UrlParameter.Optional }
);
It works fine when you for example use the following URL:
http://domain/2/Home/Index
or just simply:
http://domain/2
My problem is that when i go to "http://domain/" i get an error because i dont have specified the departmentID.
How can i add a second route which will fire when you just go to the domain.
I have tried to add the following:
routes.MapRoute(
name: "Start",
url: "/",
defaults: new { controller = "Department", action = "Select"}
);
Which doesnt work since you cant just put "/" as an URL.
I have also tried to add the default route and then just change the defaults object:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Department", action = "Select", id = UrlParameter.Optional }
);
This doesnt work either.
You have to modify your code as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Department",
url: "{deptID}/{controller}/{action}",
defaults: new { controller = "Department", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Output
Let me know if the above code still not worked for you.

Why do ASP.NET MVC URLs need Index

Struggling to work out how to have the following
http://localhost/star/regulus
the only way it will work is to have a web address of
http://localhost/star/index/regulus
This is my routes
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute( "", "star/{sstar}", new { Controller = "star", id = UrlParameter.Optional });
and in the Controller, I set up the View as :-
return View("~/views/star.cshtml", oStar);
so you can see I'm not using Index.cshtml
Any ideas how I get the top web address, not the second one.
You need to rearrange your routes
routes.MapRoute(
name: "StarRoute",
url: "star/{sstar}",
defaults: new { controller = "Star", action = "Star", sstar = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The default controller should usually be the last route registered
Assuming controller
public class StarController :Controller {
public ActionResult Star(string sstar){
//...other code that uses sstar parameter
return View();
}
}
The view should be located at...
~/Views/Star/Star.cshtml
...for the following URIs to work:
http://localhost/star/regulus
http://localhost/star?sstar=regulus
http://localhost/star/sirius
http://localhost/star?sstar=sirius

Default Routes not redirecting to expected page in mvc

I have a HomeController in which I have 2 ActionResults say Index and Projects. Now my RouteConfig.cs has below routes added:
routes.MapRoute(
name: "Default_1",//Changed this to some other name
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",//Made this name as Default
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Projects", id = UrlParameter.Optional }
);
So from the above configuration I was expecting by the default the user will redirected to /Home/Projects. But again it is redirecting to Index action. Why it is not redirecting to Projects action? Can't we have multiple routes here?Default name will not be considered as default url?
When you register your routes,put your specific route definition(s) before the default route definition. The order of route registration matters.
routes.MapRoute(
name: "Project",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Projects", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With this configuration, Any requests coming will be redirected to your Home/Projects because the url pattern is still the generic pattern({controller}/{action}/{id}). So i guess you really meant something like this.
routes.MapRoute(
name: "Project",
url: "projects/{id}",
defaults: new { controller = "Home", action = "Projects", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This will send the request http://yourSite.com/Projects to Home/Projects
Or you may use attribute routing to define this route pattern in the Projects controller.To enable attribute routing, you can call the MapMvcAttributeRoutes method in the RegisterRoutes method of RouteConfig.cs. You will still keep the default route definition there.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
And in your ProjectsController
[Route("Projects/{id?}")]
public ActionResult Index(int? id)
{
//check id value and return something
}

Routing to action getting only one ID from the URL

Im trying to make a custom router for my mvc-learning project.
I want to keep the standrard route that directs to {controller}/{action}/{id} in case of a URL like
domain.com/user/details/72
but if i get a url like this one
domain.com/2
i want to route to a specifik controller action that takes the number as an id. So i dont want the URL to specify controller and action, cause i want the url to be real short and the controller and action should always be the same.
Ive kind of made it work but i get very strange unpredictable results so i wanted to ask if im doing something very weird. Here are my routes:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DisplayKombak",
url: "{kombakId}",
defaults: new { controller = "Kombak", action = "DisplayKombak", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Try something like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DisplayKombak",
url: "{kombakId}",
defaults: new {controller = "Kombak", action = "DisplayKombak"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
}
And KombakController.cs:
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class KombakController : Controller
{
public ActionResult DisplayKombak(string kombakId)
{
return View();
}
}
}
You may be able to force this behavior by adding route constraints.
routes.MapRoute(
name: "DisplayKombak",
url: "{kombakId}",
defaults: new { controller = "Kombak", action = "DisplayKombak", id = UrlParameter.Optional },
new {kombakId = #"\d+" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new {controller = #"\D+" }
);
The constraint on the first route will prevent the route from matching unless the first route segment is an integer, the constraint on the second route will prevent the route from matching if the first route segment is not a string of letters.

Creating a custom route to map at the root of the website

My routeConfig.cs file currently has:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have an action in my home controller that responds at:
/home/trackUser/123
How can I modify my route config so this same action gets called at:
/trackUser.aspx?userId=123
Cannot check it right now, but try this:
routes.MapRoute(
name: "TrackUserAspx",
url: "/trackUser.aspx?userId={id}",
defaults: new { controller = "Home", action = "trackUser", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Categories