ASP.Net MVC5: Extra segment in routing causes issue - asp.net-mvc

to handle my routing issue i add extra segment in routing which not redirecting me to edit action.
see my routing which causing problem
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Action/Edit/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
OR
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Edit/Action/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
i test above 2 different set of routing for PageWithId but none work
see RouteLink code
#Html.RouteLink("Edit", "PageWithId",
new
{
controller = "Customers",
action = "Edit",
id = item.CustomerID,
page = ViewBag.CurrentPage
})
my edit action code
public ActionResult Edit(int page, string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
ViewBag.CurrentPage = page;
return View(customer);
}
now tell why this url http://localhost:2020/Customers/Action/Edit/1/AlFAKI not redirecting me to edit action?
see my full routing code
routes.MapRoute(
name: "PageWithSort",
url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }
);
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Action/Edit/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
OR
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Edit/Action/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Your route looks wrong, I believe you are trying to do is:
routes.MapRoute(
name: "PageWithId",
url: "{controller}/{action}/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
And your url will be: http://localhost:2020/Customers/Edit/1/AlFAKI

Related

map routing not using default action

when I enter - http://localhost:60559/movies
The browser is redirecting to movies/index
Why? I have made the default action edit. All the names are just demo.
routes.MapRoute(
"searchByName",
"Movies/edit",
new {Controller = "Movies", action = "edit"}
);
actions are
public ActionResult index(int? id) {
if(!id.HasValue)
id = 2;
return Content("id: " + id);
}
public ActionResult edit(int? id) {
if (!id.HasValue)
id = 1;
return Content(String.Format("id = {0}", id));
}
the expected result is id = 1 in the browser but it is showing id: 2
Ive tried your code and it didnt work well but
Please try the following and you code should work just fine
routes.MapRoute(
name:"searchByName",
url: "{controller}/{action}",
defaults: new { Controller = "Movies", action = "edit" }
);
if you plane to add parameters then use following
routes.MapRoute(
name:"searchByName",
url: "{controller}/{action}/{id}",
defaults: new { Controller = "Movies", action = "edit", id = UrlParameter.Optional }
);
Please Add the following
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SearchByName",
url: "Movies/{action}",
defaults: new { Controller = "Movies", action = "edit" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

One ASP.NET MVC route not working

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.

ASP MVC getting GET request instead of POST

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();
}

Set Default Route for a Controller as Index View

So I have the url:
www.mywebsite/signup/index
That works fine.
What I want to do is add a new route mapping to the RouteConfig class that allows the following url to work:
www.mywebsite/signup
I have tried:
routes.MapRoute(
name: "Signup",
url: "{controller}/",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
And:
routes.MapRoute(
name: "Signup",
url: "{controller}",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
And:
routes.MapRoute(
name: "Signup",
url: "{controller}/*",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
My RouteConfig class has been customized and the default routing when creating a new ASP.NET MVC Project has been changed, it currently looks like this:
routes.MapRoute(
name: "DefaultConstrained",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", controller = "Home", id = UrlParameter.Optional },
constraints: new { controller = ConstrollersAsCSV() }
);
routes.MapRoute(
name: "SiteLogin",
url: "{site}",
defaults: new { controller = "User", action = "Login", site = "" },
constraints: new { site = new ExcludeAndUseRegex() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "Login", id = UrlParameter.Optional }
);
Where the constraint method ConstrollersAsCSV (which I don't know what that is doing) is:
public static string ConstrollersAsCSV(string optional = null)
{
var list = GetControllerNames();
var sb = new StringBuilder();
sb.Append("(");
if (!string.IsNullOrWhiteSpace(optional))
sb.Append(optional);
foreach (var cont in list)
{
if (sb.Length > 1)
sb.Append("|");
sb.Append(cont.Replace("Controller", ""));
}
sb.Append(")");
return sb.ToString();
}
And the implementation of GetControllerNames is:
public static List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(
type => controllerNames.Add(type.Name.ToLower()));
GetSubClasses<ApiController>().ForEach(
type => controllerNames.Add(type.Name.ToLower()));
controllerNames.Add("elmah");
return controllerNames;
}
And the implementation of GetSubClasses is:
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
The error I am getting when going to www.mywebsite.com/signup is:
**HTTP Error 403.14 - Forbidden**

Asp mvc map url with hardcoded controller and action can't be found

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

Resources