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.
Related
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.
In the application I am working on, I have an Html page inside views folder and I have mentioned the action as follows.
<form name="form" onsubmit="return validateForm();" method="post" action="//Controllers/RegistrationController.cs">
The registration controller returns a view.
public ActionResult Detail(string name)
{
return View();
}
When I run the program, I get server not found error.
I also tried changing the action string to action="//Controllers/RegistrationController.cs/Detail"
but got the same error.
Does the action string have to be written in some other way?
Thank you very much in advance for your help.
Assuming you are using the default routes ({controller}/{action}/{id}) you need:
action="/Registration/Detail"
Actually I would recommend you using HTML helpers to generate forms and never hardcode them as you did:
#using (Html.BeginForm("Details", "Registration", FormMethod.Post, new { name = "form", onsubmit = "return validateForm();" }))
{
...
}
Description
You don't have to set the path like in your solution. You don't need to set Controllers because the framework knows that you mean the controller.
Assuming that you dont change the routing in global.asax, your RegistrationController.cs has an ActionMethod called Detail (decorated with [HttpPost]) and the following folder structure within your project.
Controllers/RegistrationController.cs
Views/Registration/Detail.cshtml
#using (Html.BeginForm("Detail", "Registration", FormMethod.Post, new { #onSubmit = "return validateForm();" }))
{
// Your Form's content
}
/registration/detail - you don't need to reference the path to the actual file. The framework finds the controller class and invokes the requested action for you. It uses the routes as defined in global.asax.cs to determine the controller and action from the url. The default route is {controller}/{action}/{id} where the first two have defaults of "Home" and "Index", respectively, and the third is optional. You can change this if you want by adding/modifying the route set up.
I'm want to host both WCF 4 and MVC 3 in my C#.Net project. But when I add the service paths for WCF, Html.ActionLink starts creating a wrong url for MVC app. My route table is created as:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.Add(new ServiceRoute("api1/projects", new WebServiceHostFactory(), typeof(Projects)));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Above route table creates the right access paths to both the WCF and MVC applications, but Html.ActionLink creates the edit links as
http://localhost:8000/api1/projects?action=Edit&controller=technology&id=2
instead of
http://localhost:8000/technology/Edit/2
If I omit the line starting with RouteTable.Routes.Add, the ActionLink works as expected (and of course not the WCF). How can I add the WCF routes and make sure actionlink behaviour doesn't change?
Try putting ServiceRoute registration after MapRoute.
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;
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.