Routing Issues MVC4 - c#

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

Related

Take string input on default index method in mvc5

i want to take input of username value on default mvc5 home page controller Index method. I already tried to do it like bellow [Route("{Username?}")] but it suppose to get value on url: https://localhost:44330/myusername but this url not hits the Index method. Can i do it without changing in RouteConfig.cs or how can i do it changing RouteConfig.cs even?
public class HomeController : Controller
{
[Route("{Username?}")]
public ActionResult Index(string Username)
{
return View();
}
}
RouteConfig.cs code:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can easily do it from RouteConfig.cs. Example codes are bellow:
routes.MapRoute(
name: "Default",
url: "{username}",
defaults: new { controller = "Home", action = "Index", username = UrlParameter.Optional },
namespaces: new[] { "ProjectNameSpace.Controllers" }
);

send clean url with MapRoute mvc5

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 ?

.net set landing pages from start

I have page, which content and URL changes from database. However, I want to set URL like
localhost:xxxx/MyPage
Now I got
localhost:xxxx/Pages/MyPage
which I don't need. What can I do for change it?
My route
routes.MapRoute(
name: "LandingPage",
url: "Pages/{*urltitle}",
defaults: new { controller = "Admin", action = "LandPage", urltitle = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
My code for change content and URL
public ActionResult LandPage()
{
string temp = RouteData.Values["urltitle"].ToString();
var item = RepositoryManager.Instanse.LandingContentRepository.GetItemByURL(temp);
IEnumerable<LandingContent> list = new List<LandingContent>() { item };
ViewBag.HtmlStr = item.HTMLText;
return View(ViewBag);
}
Well, I kinda solved it
my final route
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LandPage",
url: "{segment}",
defaults: new { controller = "Home", action = "LandingPage" }
And in HomeController you do this
[AllowAnonymous]
public ActionResult LandingPage(string segment)
{some logic}

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

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