ASP.NET MVC 5 Routing issue using PagedList package - asp.net

I am using the PagedList package to create a paging section for my tables.
Everything is working fine, but my urls are shown as:
http://localhost:55808/suppliers?page=2&sortOrder=id_desc&searchFilter=s
So I created a custom route for this using the following code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
// Index Suppliers with paging / search filter / orderby
routes.MapRoute(
"SupplierPaging",
"suppliers/{searchFilter}/{sortOrder}/{page}",
new
{
controller = "Suppliers",
action = "Index",
sortOrder = UrlParameter.Optional,
searchFilter = UrlParameter.Optional,
page = 1
}
);
// Default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Also this is working for the Index of my Suppliers controller.
Now if I go to the http://localhost:55808/suppliers/edit/1 page to edit a supplier, it goes into the Index method of my controller instead of the Edit.
Do I need to add all these methods as custom routes to get it working? Eg:
A custom route for edit
A custom route for delete
...
Or what am I missing here?

Add route constraints so the first route will not match for other actions:
routes.MapRoute(
"DefaultPaging",
"{controller}/{searchFilter}/{sortOrder}/{page}",
new
{
action = "Index",
sortOrder = UrlParameter.Optional,
searchFilter = UrlParameter.Optional,
page = 1
},
new
{
sortOrder = #"\d+",
searchFilter = #"\d+",
page = #"\d+"
}
);
Also I've replaced "suppliers" part of the route with "{controller}", so this will work for all controllers with paging.

Related

Routing issues in asp.net MVC 4

I am a beginner in MVC 4. I have an url ../Home/NameDetailsPage/20 where controller= Home, action=NameDetailsPage and pageIndex= 20. How do i write my route engine for this url?
routes.MapRoute(
......
.....
);
In controller, NameDetailsPage works pretty fine for default page such as int page=2 :-
public ActionResult NameDetailsPage(int? page)
{
var context = new BlogContext();
IQueryable<string> list;
list = from m in context.Blogs.OrderBy(m => m.BlogId)
select m.Name;
ViewBag.total = list.ToArray().Length;
ViewBag.page = page;
var pageNumber = page ?? 1;
ViewBag.page1 = pageNumber;
return View("NameDetails", list.Skip(pageNumber * 4).Take(4));
}
But the pageNumber is always 1 whatever pageIndex in the url. So It shows same result for all the pageIndex. How can I set pageNumber other than 1. Thanks in Advance.
When you have a route like the following
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
The defaults object properties (controller, action, id) are what can be passed to the action (if the action has parameters that match the name).
So if you have an action:
public ActionResult NameDetailsPage(int? page)
there are no values being passed from the URL as page.
You can either change {id} to {page}, or preferably:
public ActionResult NameDetailsPage(int? id)
Based on your comment you are most of the way there. You just need to define the parameter you are passing to the action. In this case switching {id} for {page} and altering the defaults to suit.
routes.MapRoute(
name: "Page",
url: "Home/NameDetailsPage/{page}",
defaults: new { controller = "Home", action = "NameDetailsPage", page = UrlParameter.Optional } );

ASP.NET MVC3 Route - example.com/blog/projects to example.com/projects

I have an ActionResult in my Blog controller that is returning all the projects called Projects, but my URL looks like example.com/blog/projects, I want to set up route to make it look like example.com/projects.
Is it possible? And if it is then how can I achieve that?
You have to register route following way Global.asax and put this as a first route
routes.MapRoute("Projects", "Projects" , new { controller="blog" , action="Projects" });
You can add a route to your Global.asax.cs file, such as:
routes.MapRoute(
"Projects", // Route name
"Projects/{id}", // URL with parameters
new { controller = "Blog", action = "Projects", id = UrlParameter.Optional } // Parameter defaults
);
So it looks something like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Projects", // Route name
"Projects/{id}", // URL with parameters
new { controller = "Blog", action = "Projects", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Make sure you put your new route above the default one otherwise yours will never be hit.

Asp.net mvc Details method

I am working on a project, where I have used the default routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/{name}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
I have a users controller and methods like Index, create and details
I dont want to create links like http://mysite/users/details/111/somename
instead i want to create http://mysite/users/111/somename
like stackoverflow kind of url.
I know I can achieve by registering a route like this (before default route)
routes.MapRoute(
"UsersDetails", // Route name
"Users/{id}/{name}", // URL with parameters
new { controller = "Users", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
but then all my other urls start creating problem. for example if i have url
http://somesite/users/registersuccess
Is there in workaround for this issue, or I should register all the urls.
Regards
Parminder
You'd need a constraint on your id parameter for your routes:
routes.MapRoute(
"UsersDetails",
"Users/{id}/{name}",
new { controller = "Users", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional },
new { id="\d+" }
);
Then your other routes wouldn't be affected.

When combining asp.net Dynamic Data and MVC MetaModel.Visible contains tables with Scaffold==false

I combined MVC and DD by creating a new DD project and adding the MVC stuff (references, routing, usings, etc).
The list of tables on default.aspx (from DD) will show all tables, including the ones with [ScaffoldTable(false)]. The URL's of the tables with Scaffold==true have the expected form (DD/TableName/List.aspx). However, the URL's of the tables that should not be shown are in the form /Home/List?Table=TableName.
If you leave out the MVC routing (Routes.MapRoute) then the tables with Scaffold(false) are not shown. Or you can leave out only the Parameter defaults.
My guess is that Dynamic Data determines if a table is visible by checking to see if a route can be made to for the List page. The DynamicDataRoute will not match because that will not generate a route if Scaffold==false. BUT THEN the MVC Route WILL match because of the Parameter defaults at the end.
Am I correct and is this a bug or am I completely missing something here?
EDIT:
I fixed it by adding filtering the VisibleTables on Scaffold like this, but that's a workaround...
System.Collections.IList visibleTables =
MvcApplication.DefaultModel.VisibleTables.Where(o=>o.Scaffold==true).ToList();
My RegisterRoutes in global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
DefaultModel.RegisterContext(typeof(studiebase2Entities), new ContextConfiguration() { ScaffoldAllTables = false });
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = DefaultModel
});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
A somewhat cleaner fix would be to add a constraint to your MVC route so that it doesn't match when a 'Table' is specified. e.g. something like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { Table = "" }
);

asp.net mvc maproute

Greetings,
I have a problem with link in mvc application. When I run it via Visual Studio it's ok. The link then is as follows:
http://localhost:2566/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
when I run it via IIS7 the link is as follows:
http://localhost/ActivationService/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
The default route is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
I assume I have to change this MapRoute, am I right? How to change it? ActivationService is my virtualDirectory in IIS. Can someone help me with this please?
I also tried to maproute as follows:
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
but also without success
Did you add the new one or replace the existing one?
If you added, you need to position it before the existing one.
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The rules take precedence..

Resources