Url.Action to show page no in url - asp.net-mvc-3

Trying like this
Url.Action("Index", "Home", new { page = 5 })
is giving me url like
/Home/Index?page=5
How to get a url like this
/Home/Index/5

By defining a route:
routes.MapRoute(
"PagedRoute",
"{controller}/{action}/{page}",
new { controller = "Home", action = "Index", page = UrlParameter.Optional }
);
And be careful with the default route (the one that uses an id) as it is similar. You will might need to put this custom route before the default one or remove the default route as it rarely be hit under those circumstances.
I would recommend you going through the Routing tutorials to gather deeper understanding of how they work.

Related

Changing Route Config impact on ajax url and embedding controller name in request (Mulitple Time)

My all ajax calls unexpectedly appending controller name twice in ajax calls by changing following line
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
to
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
I changed this line because i found a extra redirect, that first user will go to home and then from there he will be redirected to login page due to authorization failure. have a look at following console error
http://localhost:51381/Document/Document/GetCategoriesByDocumentType?categoryDocumentType=1&_=1480570187959
404 (Not Found)
as you can see that two controller name was appended.
/Document/Document/
My ajax call in separate Js file.
var url = 'Document/GetDocumentGrid?CategoryDocumentType=' +
CategoryDocumentType + '&categoryId=' + CategoryList + '&keywords=' +
search + '&isArchived=' + isArchived;
and when i revert route config , all ajax call works as expected.
what if i wanted my user to go on login page directly, instead of home? or i have to change all my ajax URL's ? And what is the reason of this behavior?
Note: i cannot use any Razor syntax as i have separate js files and i dont want to change every Js file, as my project goes almost 90 pages :(

I seem to be getting a GUID in the MvcSiteMapProvider Breadcrumb trail, not sure why?

I am using MVC3, C#, Razor, mvcSiteMapProvider V4.
I am using "Mvc.sitemap"
<mvcSiteMapNode title="Reports" controller="Report" action="Index" preservedRouteParameters="ClientId" route="Report">
<mvcSiteMapNode key="Report_Section" title="Sections" controller="Section" action="FilterByReport" preservedRouteParameters="ClientId,ReportId" route="Report_Section">
<mvcSiteMapNode key="Background" title="Background" controller="Wizard" action="Index" preservedRouteParameters="ClientId,ReportID,SectionID,SectionName" route="Background"/>
The "Global.asax" custom routes look like:
routes.MapRoute("Report", "Report/{ClientId}", new { controller = "Report", action = "Index", ClientId = UrlParameter.Optional });
routes.MapRoute("Report_Section", "Report/{ClientId}/Section/{ReportId}", new { controller = "Section", action = "FilterByReport", ReportId = UrlParameter.Optional });
routes.MapRoute("Background", "Report/{ReportID}/SectionID/{SectionID}/SectionName/{SectionName}", new { controller = "Wizard", action = "Index", ReportID = UrlParameter.Optional, SectionID = UrlParameter.Optional, SectionName = UrlParameter.Optional });
The "Report" and "Report_Section" routes work fine. However when I go into the "Background" route I lose all of my route structure, for the "Report_Section" and "Report" routes, in the mvcSiteMap BreadCrumb URL. Instead I get a GUID ie:
http://localhost/7ebe9bb9-a663-43fd-9fb1-865866be12b9
I believe this might be the XML Node Key that is autogenerated. However it prodocues a 404 when clicked.
I should get something like:
Reports
http://localhost/Report/10
Sections
http://localhost/Report/10/Section/100
Any ideas what could be causing this?
Thanks.
You will get a Guid for the URL if the URL resolver cannot find a match. Throwing an exception in this case was causing other issues (see why does URL resolver throw an exception).
However, I can't tell you exactly why it is not matching without seeing more of your configuration. A clue is that we are using UrlHelper.RouteUrl(string, RouteValueDictionary) to resolve the URL. You can try calling it explicitly with your route values and route name. Also, see the source code for other clues.
One thing I noticed that looks off is that you are preserving the ClientID route parameter for Background when it is not even used in that route. Keep in mind that preserving route parameters only copies them from the current HTTP request and your other nodes will appear to forget them.
PreserveRouteParameters is typically used for CRUD operations where you make data editing pages. If you want it to appear to "remember" the user's navigation path, then you need to add 1 node per unique route value combination to your sitemap.
If the above doesn't help, please create a small demo project that exhibits the problem and either upload it to GitHub or else zip it and make it available for download, then open a new issue at GitHub with the link to the demo.

Why #Html.ActionLink("Home", "Index", "home") generates Home

When I use Html.ActionLink, I found that #Html.ActionLink("Home", "Index", "home") generates this string Home, so why the address \home\index becomes \, I found this is related with default route which setted in web.conf. I have viewed the mvc 3 source code, but I can't find answer. Who can help me? Thanks.
That's due to the default route. If you don't specify default action and controller it will generate /home/index as url.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { id = UrlParameter.Optional }
);
routing is bidirectional:
- in: it converts incoming urls into controller/actions/parameters
- out: it converts controller/actions/parameters into urls

Change default view in VS 2010

How to set change a default View in MVC3 when debbuging the project in Visual Studio 2010.
As soon I hit F5, The default View it opens is Localhost/Home/Index.
Where is it being set, How do I update it?
Can anyone shed some light on this please? It is not straight forward(for me though).
Thank you
All you have to do is changed your default MapRoute parameters. Typically, this is what you'll see by default as your Default route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }); // Parameter defaults
Just change the controller property and the action property to what you want your default to be. For instance, you could do:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "AnotherController",
action = "aDifferentAction",
id = UrlParameter.Optional }); // Parameter defaults
All that is changed here is the controller and action properties. Now when you browse to just the qualified name, it will go to your AnotherController.aDifferentAction() method, instead of the HomeController.Index() method.
Explanation
The reason why it defaults to Home.Index(), is because that is the first matched route when you have empty route parameters for controller and action. By changing these defaults in the MapRoute() call, you are telling routing that if nothing is there for the route parameters, go to AnotherController.aDifferentAction() action method.
As long as this is the first route, you should be set.
You can set the default page in the Routes table as Shark suggests, but something tells me this probably isn't really what you're looking for. If you just want to debug a specific page, right-click the view and select 'View In Browser' from the context menu.

mvc application with razor data view engine problems with the url

I create a new mhc application with razor data view engine. I have a problems with the url
Here is my action links
#Html.ActionLink("Home", "Index")
#Html.ActionLink("Schedule", "Schedule")
After I loaded home page my url looks fine
Example: mysiteurl.com
Then I click Schedule link (if I hover I see the correct url http://mysiteurl.com/home/schedule). If I click it as a result my url will http://mysiteurl.com//#/Home/Schedule. I don't know why its adding # sign to my url but it's causing the issue in my application with other pages.
any idea what I'm doing wrong?
I don't have any custom routing
here is my RegisterRoutes method in Global.asax
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 = UrlParameter.Optional } // Parameter defaults
);
}
Your ActionLinks look fine. I'd guess you have at least one custom route defined inside your RegisterRoutes method. I'd guess you've got a typo somewhere in there.
You could try installing Glimpse via NuGet - it allows you to see exactly which routing rules are firing. Scott Hanselman has this blog on how to use it.

Resources