how to modify the routes? - model-view-controller

I have asp.net mvc project, and it's structure and routes are as below.
"Site.Master" include images and css files,with paths:
../../Images/1.gif
../../Content/site.css.
When visiting the page "http://www.localhost.com/Info/Index/1001", it works.
But page "http://www.localhost.com/Info/Index/1001/1" or "http://www.localhost.com/Info/Index/1001/2", don't.
I modified the images and css file path in the Site.Master like:
/Images/1.gif
/Content/site.css
In addition, is there another way to fix it? or modify routes? Because, i want deploy it using virtual directory in iis.
-> Images
1.gif
2.jpg
-> Content
site.css
-> Views
Home
-- index.aspx
Info
-- index.aspx
Shared
-- Site.Master
routes.MapRoute(
"InfoPagedRoute",
"{controller}/{action}/{classid}/{page}",
new { controller = "Info", action = "Index", classid = #"\d{1,10}", page = 1 }
);

Your original route:
routes.MapRoute(
"InfoPagedRoute",
"{controller}/{action}/{classid}/{page}",
new { controller = "Info", action = "Index", classid = #"\d{1,10}", page = 1 }
);
It looks like you are trying to use a validator in your route defaults for the classid, so either you need to include a validation parameter or set a default value for the classid.
I would suggest:
routes.MapRoute(
"InfoPagedRoute",
"{controller}/{action}/{classid}/{page}",
new { controller = "Info", action = "Index", classid = 1001, page = 1 }
);
If updating the route doesn't resolve the issue, you could use Phil Haack's route debugger. It is really helpful in figuring out if the route you are expecting to get hit is actually the one that is being used.

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 :(

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.

How do I route to /Configuration/ URL in ASP.NET MVC3?

This is for an ASP.NET MVC3 web application. In RegisterRoutes I have the following as my only line:
routes.MapRoute("Default", "Configuration", new { controller = "DeviceConfiguration", action = "Index" });
When I run the project, going to the URL /Configuration/ gives me a 404 error. However if I change the word Configuration to any other word, such as:
routes.MapRoute("Default", "Configuratio", new { controller = "DeviceConfiguration", action = "Index" });
Then going to the URL /Configuratio/ loads just fine. It seems as if ASP.NET is simply refusing to route to the URL /Configuration/.
Again, this is the only line in RegisterRoutes; I've tried commenting out everything else to debug this. I have no MapRoute or IgnoreRoute calls in my code anywhere else, and I am not editing the routing table in any location.
How can I change this behavior?
I suspect that you have a physical folder called Configuration under the root of your application. The ASP.NET MVC routing engine has preference for physical folders over routes. One possible way is to set the RouteExistingFiles property to true after your route definition:
routes.MapRoute(
"Default",
"Configuration",
new { controller = "DeviceConfiguration", action = "Index"
});
routes.RouteExistingFiles = true;

Url.Action to show page no in url

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.

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