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 }
Related
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
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 a Login controller with an Index view that I want rendered at the url http://sitename/login. It works with I browse to http://sitename/login/index but I want to omit index from the URL. Here's my route config (default):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
}
.. and the controller:
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
}
If you had left the Default route alone, it would do what you ask. You just have to set the default value for the action route value as Index.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Alternatively, you can insert an additional route to cover your Login controller.
routes.MapRoute(
name: "Login",
url: "login/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
A third option is to rename your LoginController.Index method to LoginController.Dashboard_1, since that is your default action name for every controller in the application.
I have two route with same signature but only the parameter names are different how to fix this issue.
Following is my code
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultRoute2",
url: "{controller}/{action}/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
The routes are meant to accept different URL structures. Both of your routes have same structure, so the first one will always match, and the second will never be tested.
Instead of using a different route, in /Employee/Index you should just use the parameter id.
public class EmployeeController : Controller
{
public ActionResult Index(string id)
{
string formSubmissionId = id;
}
}
The URL for that action would be the same that (I believe) you wanted to achieve with the second route: Employee/Index/id
UPDATE
I've just realized. If you only need the parameter formSubmissionId for the action /Employee/Index you could do this:
// Note the order of the routes:
routes.MapRoute(
name: "DefaultRoute2",
url: "Employee/Index/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
public class EmployeeController : Controller
{
public ActionResult Index(string formSubmissionId)
{
// ...
}
}
Now i fix this issue as
routes.MapRoute(
name: "DefaultActivity",
url: "{controller}/LoadActivity/{formSubmissionId}",
defaults: new { controller = "{controller}", action = "LoadActivity", formSubmissionId = UrlParameter.Optional }
);
this is work around of my scenario.
I have only default route enabled:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Which will resove paths like: hostname/Home/Index, hostname/Home/Foo, hostname/Home/Bar/23 just fine.
But I must also enable route like this: hostname/{id} which should point
to:
Controller: Home
Action: Index
{id}: Index action parameter "id"
Is such a route even possible?
If id is a number you could add a route above the other one and define a regex constraint:
routes.MapRoute(
name: "IdOnlyRoute",
url: "{id}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { id = "^[0-9]+$" }
);
Not Tested :
Create your route like this
routes.MapRoute(
name: "custom",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
under your default route
Since I'm short on time, I have configured route for every action. Luckly there are not many of them. :)
routes.MapRoute(
name: "IndexWithParam",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "IndexWithOutParam",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Login",
url: "Home/Login",
defaults: new { controller = "Home", action = "Login" }
);
So last route was repeated for ever other action. Not sure if it can be done better, but it works.
Have you tried to play around with AttributeRouting package?
Then your code will look like this
[GET("/{id}", IsAbsoluteUrl = true)]
public ActionResult Index(string id) { /* ... */ }