I have next specific map routes
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "MyController", action = "MyPostAction" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "MyController", action = "MyGetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
my default controller looks like
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
and the issue is next:
my get MyPageGet route show a page with include FORM with POST reqvest to MyPagePost route, but on a first call I am getting the same GET request and see in URL other extra param ?culture=de. Moreover, with or without this parameter, second call it working fine via MyPagePost route.
UPDATE:
In Chrome or fiddler Logs I see that reqvest to URL-Up/Bla-Bla has 302 status and response heared is URL-Up/Bla-Bla?culture=de. Why it can't be processed ?
just try it with
#using(Html.BeginRouteForm("MyPagePost",FormMethod.Post))
{
<input type="submit" value="Submit"/>
}
The routes in your post working for me in both html.beginform and html.beginrouteform on the first time.
i try it with the following routes and action methods
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "Home", action = "PostAction" },
new { name="[A-Za-z].+", httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "Home", action = "GetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
public ActionResult GetAction()
{
return View();
}
[HttpPost]
public ActionResult PostAction()
{
return View();
}
Related
I have a problem with routing, RouteConfig.cs contains these routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TermsOfService",
"termsofservice",
new { controller = "Home", action = "TermsOfService" }
);
routes.MapRoute(
"PrivacyPolicy",
"privacypolicy",
new { controller = "Home", action = "PrivacyPolicy" }
);
routes.MapRoute(
"Contact",
"contact",
new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
"Support",
"support",
new { controller = "Home", action = "Support" }
);
routes.MapRoute(
"ReadOurStory",
"readourstory",
new { controller = "Home", action = "ReadOurStory" }
);
routes.MapRoute(
name: "BlogItem",
url: "blog/{name}",
defaults: new { controller = "Home", action = "BlogItem" }
);
routes.MapRoute(
name: "ProductDetail",
url: "products/{name}",
defaults: new { controller = "Home", action = "Product" }
);
routes.MapRoute(
name: "Products",
url: "products",
defaults: new { controller = "Home", action = "Products" }
);
routes.MapRoute(
name: "Tutorials",
url: "tutorials",
defaults: new { controller = "Home", action = "Tutorials" }
);
routes.MapRoute(
name: "Blog",
url: "blog",
defaults: new { controller = "Home", action = "Blog" }
);
routes.MapRoute(
name: "TutorialDetail",
url: "tutorials/{name}",
defaults: new { controller = "Home", action = "Tutorial" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and these two actions in HomeController:
public ActionResult Products()
{
var model = LoadItemsModel(ItemType.Product);
return View(model);
}
public ActionResult Tutorials()
{
var model = LoadItemsModel(ItemType.Tutorial);
return View(model);
}
Now the link for products is working:
http://localhost:61296/products/
but the link for tutorials:
http://localhost:61296/tutorials/
returns error
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory
I tried to change the route just for a test to tutorials2 and the action to Tutorials2 and then this modified link is working. I do not know why the route tutorials does not work.
Based on the error, my guess would be that you have a physical directory in your project folder called "tutorials". Any physical files/directories will always overrule any routes. Then, since directory listing is disabled by default, you get that 403 error. Remove or rename the physical directory and you should be fine.
I have three url types. These:
first: http://localhost/
second: http://localhost/X/
third: http://localhost/X/Y/
Examples Url:
http://localhost/test/
http://localhost/test/details/
first:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
second:
routes.MapRoute(
"Module",
"{module_name}/{controller}/{action}",
new
{
controller = "Module",
action = "Index",
module_name = UrlParameter.Optional
}
);
public class ModuleController : Controller
{
//
// GET: /Module/
public ActionResult Index(string modul_name)
{
return View();
}
}
third:
routes.MapRoute(
"ModuleDetails",
"{module_name}/{details_param}/{controller}/{action}",
new
{
controller = "ModuleDetails",
action = "Index",
module_name = UrlParameter.Optional,
details_param = UrlParameter.Optional
}
);
public class ModuleDetailsController : Controller
{
//
// GET: /ModuleDetails/
public ActionResult Index(string modul_name, string details_param)
{
return View();
}
}
in this instance;
http://localhost/X/
response: "Home", "Index"
but;
http://localhost/X/
response: Application in the server error. Resource Not Found.
http://localhost/X/Y/
response: Application in the server error. Resource Not Found.
How can I do?
Thanks, best regards..
http://localhost/X/
response: Application in the server error. Resource Not Found.
This happens because each of your routes specifies at least 2 mandatory parameters.
Try to add this one:
routes.MapRoute(
"Default",
"{controller}/{action}",
new
{
controller = "Home",
action = "Index"
}
);
that's right friends..
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Module",
"{modul_name}",
new { controller = "Modul", action = "Index", modul_name = UrlParameter.Optional }
);
routes.MapRoute(
"Page",
"{modul_name}/{page_name}",
new { controller = "Page", action = "Index", modul_name = UrlParameter.Optional, page_name = UrlParameter.Optional }
);
I have next map url definition where I want to set specific action and controller for special url:
//localhost:55321/SpecificPart/UserTextId all other routes should work in default way
but based on Route Debugger my URL is mapping to my main route rule how to handle this case ?
routes.MapRoute(
"PartnerSighUp",
"SpecificPart/{id}",
new { controller = "SpecificController ", action = "SpecificAction" },
new { id = "[A-Za-z].+" }
);
routes.MapRoute(
name: "EnglishHomePage",
url: "",
defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional }
);
Update if SpecificPart = SpecificController (controller name) it is working but if SpecificPart != SpecificController and looks to other controller it is not working
I use RedirectToRoute method to force culture in the url. All was working until I create an Admin area in my project. Now the method redirects to the Area controller.
Example with the main HomeController : /Home/Contact
public ActionResult Contact()
{
Response.RedirectToRoute(RouteData.Values);
return View();
}
The method redirects to /Admin/Home/Contact, the values of RouteData.Values before the redirection are :
[0] "Controller" Home
[1] " Action" Contact
My Main Route :
routes.MapRoute("Default_culture", "{culture}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional , new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Controllers" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Controllers" });
Route in RegisterArea method :
context.MapRoute(null, "{culture}/Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Areas.Admin.Controllers" });
context.MapRoute(null, "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Areas.Admin.Controllers" });
I don't understand this behaviour. What am I doing wrong?
I just fixed this problem in my site.
Change Response.RedirectToRoute(RouteData.Values);
To Response.RedirectToRoute("Default", RouteData.Values);
I have routes below in my MVC4...
routes.MapRoute("Account", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("Admin", "Admin/{action}", new { controller = "Admin", action = "Index" });
routes.MapRoute("Custom", "{action}", new { controller = "Home", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
...and need to find a way to route all url which has 2 symbol(www.domain.com/word2pdf) in url to action Index and pass url path(word2pdf) as api parameter.
public ActionResult Index(string api)
{
}
Any ideas how to do that?
As #Murali said, the proper way is to use Attribute Routing, but that's a new feature for mvc5 only.
If you want or have to stay on mvc4:
routes.MapRoute(name: "SomeRoutingName",
url: "{api}",
defaults: new { controller = "SomeControllerName", action = "Index"},
constraints: new { api= ".+2.+"});
Also just for some lol's, this should also work:
routes.MapRoute(name: "SomeRoutingName",
url: "{partA}2{partB}",
defaults: new { controller = "SomeControllerName", action = "Index" });
In Controller:
public ActionResult Index(string partA, string partB)
{
var api = string.Concat(partA,"2",partB);
}