send clean url with MapRoute mvc5 - c#

I'm new in mvc5, and I don't understand well how to form a clean url.
Right now my RouteConfig.cs file looks like :
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: "Categorie",
url: "{urlFormer}",
defaults: new { controller = "ListOrGrid", action = "ListOrGrid", id = UrlParameter.Optional, urlFormer = UrlParameter.Optional }
);
}
}
And I call this with :
#Url.Action("ListOrGrid", "ListOrGrid", new { id = cat.Categorie_Id, urlFormer = GlobalFunction.nomCategories(cat.Categorie_Id) })
The function GlobalFunction.nomCategories(short id) transform the id of categories in my table into a String
and my urls have this form : https://localhost:44375/ListOrGrid/ListOrGrid/12?urlFormer=Mode%2FFemmes%2FVetements%2FJupes
But I wanting the url looks like : https://localhost:44375/Mode/Femmes/Vetements/Jupes
How can i send my categorie id through the route "Categorie" to a controller without print it in the url like the "default" MapRoute ?

Related

MVC 5 Routing issue ActionResult root

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 }
);
}
}

custom Routing from controller level in MVC5?

I am trying to defined custom routing in MVC5 like below. But when I call http://company.com/protected/Myaccount is not working. What I am doing wrong.
and also how should defined default load with http://company.com/protected. Now it is loading http://company.com/
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Protected",
url: "protected/{controller}/{action}/{id}",
defaults: new { controller = "MyAccount", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "MyAccount", action = "Index", id = UrlParameter.Optional});
}
[RoutePrefix("Protected")]
[Route("{action=index}")]
public class MyAccountController : Controller
{
// GET: MyAccount
public ActionResult Index()
{
...
}
}
Edit: I forgot the second part of your question.
[RoutePrefix("Protected")]
public class MyAccountController : Controller
{
[Route("Index")] //Route: /Protected/Index
[Route("")] //Route : /Protected
[Route("~/")] //Route : /
public ActionResult Index()
{
return View();
}
}
With [RoutePrefix] you change the access to your controller, with [Route] you can change your access to your action (ex:parameter and order) and defined default for the controller and your app.
In this case, i delete the "Protected" route on RegisterRoute because it's not used. The RouteAttributes is another solution
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//routes.MapRoute(
// name: "Protected",
// url: "Protected/{Controller}/{action}/{id}",
// defaults: new { controller = "MyAccount", action = "Index", id = UrlParameter.Optional }
//);
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
}
In fact, attributes routing is another way to define routing, it's more flexible.

Routing Issues MVC4

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 }
);
}

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.

URL Rewriting on my ASP .NET MVC website

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 }
);
}
}

Categories