I have a second thing in my 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 });
}
I need to change controller, but the problem is that it is placed in Areas.
I tried to change MapRoute to this:
url: "{area}/{controller}/{action}/{id}",
defaults: new { area="Purchase", controller = "Card", action = "Index", id = UrlParameter.Optional });
but it's not working.
Also I have this in AreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Purchase_default",
string.Format("{0}/{{controller}}/{{action}}/{{id}}", AreaNames.Purchase),
new { controller = "Card", action = "Index", id = UrlParameter.Optional });
}
And here I have seen something similar but I didn't got how to use it.
What am I doing wrong and how should it be done?
Related
I have defined a couple of static routes which look like following:
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
And third is the dynamic route which looks like following:
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
I would like to have defined these two static routes for my Index controller:
/Index/Tutorials
/Index/LogOut
Every other route should point to:
/Index/{id}
The way I defined it right now works for 2 static routes, but when I try to pass parameter like this which isn't one of the two static routes like following:
http://localhost:60617/Index/12345/
Where 12345 is ID, I get following error:
The resource cannot be found.
How can I define these routes properly ? Can someone help me out ?
Here is the route class:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
routes.MapRoute(
name: "ResetPwdRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
);
}
}
The order in which you map routes are important.
Generic routes should be mapped after more specific routes to avoid route conflicts.
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
routes.MapRoute(
name: "ResetPwdRoute",
url: "User/ResetPwd/{id}",
defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
I've question about routing in MVC
i've created a simple website i defined a controller called HomeController.cs
defined my public ActionResult WebPage(int id)
the id is the Id of the page i'm displaying my route will look something like:
Current:
http://localhost:5000/Home/WebPage/1
What i want is:
http://localhost:5000/1
My current RouteConfig.cs
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 }
);
}
I've tried allot with different routes in RouteConfig.cs but im stuck can someone point me into the right direction?
I've searched on StackOverflow something similer to this but didn't find.
You should try route attribute, it may help you
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 });
}
Use attribute like this
[Route(“{id:int}”)]
public ActionResult WebPage(int id) { … }
What Felipe proposes will work, if you apply a constraint on the id value.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "WebPage",
url: "{id}",
defaults: new { controller = "Home", action = "WebPage" },
constraints: new { id = #"\d+" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
The answer
in my controller: [RoutePrefix("")] and my Method WebPage [Route("{id?}")]
[RoutePrefix("")]
public class HomeController : Controller
{
[Route("{id?}")]
public ActionResult WebPage(string id)
{
return View();
}
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "WebPage",
url: "{id}",
defaults: new { controller = "Home", action = "WebPage" }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index",id = UrlParameter.Optional }
);
}
}
I want to have my URL to be /{page number} for my home page for example localhost:50915/1 for page 1 for a list of records, my URL routing is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "EditForm",
url: "{controller}/{formid}",
defaults: new { controller = "ManageForm", action = "Update", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home",
url: "{page}",
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 second MapRoute method is the one which I added to perform the desirable action. It works perfectly fine, but whenever I try to access another Controller "ManageController" for e.g localhost:50915/ManageForm it returns me the home page (same page showing the list of records).
Here is a snapshot of my solution:
Whenever I tried to remove the lines of codes which perform this URL rerouting for the Home Page pagination, everything works fine, but of course the URL will be as localhost:50915?page=1
My HomeController is as follows:
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ViewResult Index(int page = 1)
{
const int pageSize = 4;
FormCollection forms = FormService.GetAllActiveForms(page, pageSize);
var formModel = new FormListViewModel
{
Forms = forms,
PagingInfo = new PageInfo
{
PageNumber = page,
PageSize = pageSize,
TotalItems = forms.Count > 0 ? forms[0].TotalRecord : 0
}
};
return View(formModel);
}
}
Help much appreciated thanks.
You cannot use localhost:50915/ManageForm cause it conflicts with another route definition. MVC does not see the difference between "{controller}/{formid}" and "{page}" when your route parameter is optional.
You have to use your first route as localhost:50915/ManageForm/{formId} to get it working.
Solution would be to map a new routing for the manage form to result in the url /ManageForm/Create
I have changed the RouteConfig.cs as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//<host>/{page}
routes.MapRoute(
name: "Home",
url: "{page}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//ManageForm/Create
routes.MapRoute(
name: "CreateForm",
url: "{controller}/Create",
defaults: new { controller = "ManageForm", action = "Index", id = UrlParameter.Optional }
);
//ManageForm/{formid}
routes.MapRoute(
name: "EditForm",
url: "{controller}/{formid}",
defaults: new { controller = "ManageForm", action = "Update", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
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.
I'm working on an image viewer, so I've a controller named Viewer and when I pass it routeValues it is passed in the URL like this :
http://www.mywebsite.com/Viewer?category=1&image=2
Here is the link used to get on this page :
#Url.Action("Index", new { category = p.Category, image = p.Image })
But I do like to have this URL :
http://www.mywebsite.com/Viewer/1/2
I have tried to do a few tricks in the RegisterRoutes method of the RouteConfig class but I can not get the previous result.
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 }
);
routes.MapRoute(
name: "Viewer",
url: "{controller}/{action}/{category}-{image}",
defaults: new { controller = "Viewer", action = "Index", category = 1, image = 1 }
);
}
}
Does anyone know where I can do this?
Thanks a lot !
You need to put your more specific route before the default route, because the routes are evaluated in the same order in which you declared them:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Viewer",
url: "{controller}/{action}/{category}/{image}",
defaults: new { controller = "Viewer", action = "Index", category = 1, image = 1 }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}