I have an issue and cant seem to resolve it. I have a url, thats like:
myurl/?culture=fr
what I want is
myurl/fr
My Controller looks like :
public ActionResult Index(string culture = null)
and my routeConfig:
routes.MapRoute(
name: "Languages",
url: "{controller}/{action}/{culture}"
);
This results in the page isn't redirecting properly.
Any hints to solve it?
Give default Controller and action in your route if you are appending "fr" to the root of the URL(www.yourUrl.com/fr). like this :-
routes.MapRoute(
name: "Languages",
url: "{controller}/{action}/{culture}",
defaults: new { controller = "Home", action = "Index"}
);
Replace "Home" with your default controller and "Index" with default action.
Related
I have a controller "Home" and 2 actions inside it called "DeliveryReturn" and one called "BasketReturn" like this:
public ActionResult DeliveryReturn(string id)
{
return View();
}
public ActionResult BasketReturn(string id)
{
return View();
}
".../Home/Return/Delivery/10" -> DeliveryReturn should be called.
".../Home/Return/Basket/10" -> BasketReturn should be called.
I tried setting up my route somewhat like this. I am really lost though and it seems like it doesn't make sense at all.
routes.MapRoute(
name: "Return",
url: "Home/Return/{type}/{id}",
);
Update:
I figured it out, that I could do something like this. However, this would require me to register two routings. How would I do it with only one?
routes.MapRoute(
name: "DeliveryReturn",
url: "Home/Return/Delivery/{id}",
defaults: new { controller = "Home", action = "DeliveryReturn", id= ""}
);
routes.MapRoute(
name: "BasketReturn",
url: "Home/Return/Basket/{id}",
defaults: new { controller = "Home", action = "BasketReturn", id= ""}
);
When I run my web app it doesn't work because the default URL it opens is http://localhost/WTM/. For some reason that url gives errors but http://localhost/WTM/Home/ works fine, even though it seems to be trying to open the same page.
How do I get the standard url to redirect automatically to url/home? or how do I do the opposite? set my default to the url without /home)
This is my route config code:
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 }
);
}
Can you provide more information on the error?
As you can see by the routes provided; the controller (if not specified) will default to Home and the action to Index.
With that in mind http://localhost/WTM, http://localhost/WTM/Home, and http://localhost/WTM/Home/Index should all go to the same controller action.
Here you need to add another Route which would look like
routes.MapRoute(
name: "WTM",
url: "WTM/{id}",
defaults: new { controller = "WTM", action = "Home"}
);
Now if you try http://localhost/WTM/ this will redirect you to Home action of that controller.
I set a route map in RouteConfig.cs like:
routes.MapRoute(
name: "Default",
url: "prefix/{controller}/{action}",
defaults: new { controller = "MainView", action = "Index" }
);
And I want to do a redirect in MainView/Index under some situation, so I write code like below, and return it:
RedirectToRouteResult redirect = new RedirectToRouteResult(
new System.Web.Routing.RouteValueDictionary(new { action = "Home", controller = "Account"}));
I wish it would return the url like: localhost/prefix/Account/Home, but the result is localhost/Account/Home. How can I make the redirect result to be localhost/prefix/Account/Home?
Thanks!
All right, I made a stupid fault, there is [Authorize] attribute on this action, so the return url is made by this attribute. I correct this code and get the expected url.
trying to get started with ASP.NET MVC.
I encountered a few difficulties while setting up basic routes.
My routes are as follows:
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: "ImproItem",
url: "{controller}/{action}/{type}",
defaults: new { controller = "ImproItemForm", action = "Index", type = UrlParameter.Optional }
);
}
My view calls :
<li>#Html.ActionLink("linkLabel", "Index", "ImproItemForm", new { type = "blablabla" }, null)</li>
My controller action sig is:
public class ImproItemFormController : Controller
{
...
public ActionResult Index(String t)
{
...}
}
The view generates the following HTML:
<li>linkLabel</li>
This looks OK to me.
However, this link correctly calls the controller's action (using ImproItem route) but it does not pass the blablabla argument. The parameter t = null when I debug the app.
Can you explain me why?
What should I change to correctly receive the blablabla argument?
Now if I start the application and try to browse
Also is it normal that when I browse:
http://localhost:55193/ImproItemForm/Index?id=foobar
It does call the ImproItemFormController.Index(String t) method ?
I did not expect that this URL would match with this route:
routes.MapRoute(
name: "ImproItem",
url: "{controller}/{action}/{type}",
defaults: new { controller = "ImproItemForm", action = "Index", type = UrlParameter.Optional }
);
I thought the argument needs to have the same name than in the route : type and not id.
Thx in advance for your help.
I thought the argument needs to have the same name than in the route :
type and not id.
Actually when you request the URL - http://localhost:55193/ImproItemForm/Index?id=foobar, it actually calls the Default route only, and not the custom route that you have created. Default route has - controller name, action name and id. That means, if there is any URL matching this pattern (i.e., {controllername}/{actionname}/{id}) would match the default route.
Order of routes are very important in route collection because route table is built top-to-bottom, so as soon as the URL finds its first matching route, it stops scanning further.So ideally, default route should be the bottom most route in route collection.
I guess all your problems for this particular scenario should be resolved by performing following two steps-
Move default route to the bottom in route collection in RouteConfig.cs
Rename parameter "t" to "type" in Index action.
Change the name of the parameter in your action to match the name in the ActionLink
public ActionResult Index(String type)
I'm having a problem with MVC 4, and I guess it's something really trivial, but it's been bugging me for the last day and I can't seem to figure it out.
I have this url:
http://www.example.com/my-dashed-url
I have a Controller named:
public class MyDashedUrlController: Controller
{
}
I have only two Routes like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("my-dashed-url",
"my-dashed-url/{action}",
new { controller = "MyDashedUrl", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
I get to the index just fine. However, when I do this:
public ActionResult Index()
{
if (NoUserIsLoggedOn)
return RedirectToAction("Logon", "MyDashedUrl");
return View();
}
public ActionResult Logon()
{
Contact c = GetContact();
return View(c);
}
It doesn't redirect me to the "Logon" action properly.
It should redirect me to:
http://www.example.com/my-dashed-url/logon
but instead it tries to redirect me to:
http://www.example.com/logon
... which doesn't work (404 Not Found)
I'm missing something. Can anyone spot it? If anyone needs any more information, let me know.
And it's EVERY RedirectToAction that does the same thing in this controller. A Html.BeginForm("Logon", "MyDashedUrl") would also generate:
http://www.example.com/logon
I guess it has to do something with the Routes I defined, but I can't find the faulty one, seeing as they're all the same. If I disable all of my Routes besides the default one from MVC, the problem remains the same
Make sure that you have declared this custom route BEFORE the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"my-dashed-url",
"my-dashed-url/{action}",
new { controller = "MyDashedUrl", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Remember that routes are evaluated in the order you declared them. So the first route that matches a request will be used. If you declare your custom route after the default one, it is the default route that will match the request.