Here is my action method in TeamController...
public class TeamController : BaseController
{
// GET: Team
public ActionResult Index(int id)
{...}
}
Here is the entire route configuration:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If I use /Team?id=1 it works. If I use /Team/1 I get a 404. Using RouteDebugger it shows me that /Team?id=1 is converted to /Team/Index/1 which is correct but shouldn't the index method be invoked by default?
Thanks to the comments above I fixed it by adding this attribute to the Index method
[Route("Team/{id:int:min(1)}")]
public ActionResult Index(int id)
{..}
Related
why use custom routing in asp.net MVC
for example
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Enable Routing
routes.MapMvcAttributeRoutes();
//custom route for about page
//routes.MapRoute(
// name:"about",
// url: "Home/About",
// defaults: new { controller = "Home",action= "About", id=UrlParameter.Optional}
// );
//custom route for contactus page
//routes.MapRoute(
// name: "about",
// url: "Home/ContactUs",
// defaults: new { controller = "Home", action = "ContactUs", id = UrlParameter.Optional }
// );
//default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
HomeController.cs
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
//[Route("Home/About")]
public ActionResult About()
{
return View();
}
//[Route("Home/ContactUs")]
public ActionResult ContactUs()
{
return View();
}
}
Index.cshtml
IndexPage
About.cshtml
AboutPage
Contactus.cshtml
ContactusPage
when I run the project then write the URL manually then also give output then why use the routing attribute
home/index
home/about
home/contactus
I comment the route attribute and custom route code and above URL give the proper output then why use route attribute
my question is without route attribute easily run the action method then why need to use route attribute above the controller
If you are happy with the default routes, then you don't need to use the route attributes, or put any custom routes in the RouteConfig.
You can add routes to customise how users get to your pages, either through parameters for more dynamic pages, or to make page urls more friendly - for example:
//make about us page url "/about"
routes.MapRoute(
name:"about",
url: "about",
defaults: new { controller = "Home",action= "About"}
);
//make a product page expect an id param in the url
//for example "/catalog/product/pid1"
//"/catalog/product/pid2"
//"/catalog/product/pid3"
//"/catalog/product/pid4" all match this route
routes.MapRoute(
name:"product",
url: "catalog/product/{productId}",
defaults: new { controller = "Catalog",action= "Product"}
);
The same can be achieved in route attributes:
[Route("about")]
public ActionResult AboutUs()
{
return View();
}
[Route("catalog/product/{productId}")]
public ActionResult GetProduct(string productId)
{
//Get product, build view data etc...
return View();
}
I have a controller called "User" and also is "Index". My action has one parameter.
I want to access this action to type user/id.
I write following
routes.MapRoute(
name: "user",
url: "{controller}/{id}",
defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional}
);
but not could get any.
I have created a sample as per your question and is working fine for me.
RouteConfig.cs code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "user",
url: "{controller}/{id}",
defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional }
);
}
User Controller Code:
public class UserController : Controller
{
// GET: User
public ActionResult Index(int id)
{
return View();
}
}
Here in this code if i call http://localhost:18218/user/12 I am getting id value as 12 in my ActionResult parameter. can you please check if it meets your requirements.
I have an ASP.Net MVC 5 site where I changed the routes in the Home controller to remove the Home portion.
My Home Controller looks like
[Route("Index")]
public ActionResult Index()
{
ViewBag.Title = "Home";
ViewBag.Current = "Home";
return View();
}
This works great when I go to http://localhost:29033/Index but when I go to http://localhost:29033 I get the following error:
A public action method 'Index' was not found on controller 'MyProject.Controllers.HomeController'.
My RegisterRoutes looks like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("elmah.axd");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Any help would be appreciated.
Given that it appears that attribute routing is being employed here I believe you need to update your routes to get the desired behavior
[RoutePrefix("home")]
public class HomeController : Controller {
[Route("Index")] // Matches GET home/index
[Route("~/", Name = "root")] //Matches GET /
public ActionResult Index() {
//...code removed for brevity
}
}
Reference Attribute Routing in ASP.NET MVC 5
I have my Home controller set up like this, going into different functions depending on the parameters it recieves.
Problem is in my home controller, it treats "gametwo" as a query for my route on my home controller.
Example
mysite.com/serchsomething <-- This will search the given string
mysite.com/gametwo <-- This also searches instead of going to gametwo controller
I have normal routeconfig.cs file, with just added attributeroutes.
What is the best way of dealing with routes with multiple parameters? So that they wont be ambigious or crash with any other routes? thanks
home controller
public ActionResult Index()
{
...
}
[HttpGet]
[Route("{Query}")]
public ActionResult Index(string Query)
{
...
}
[HttpGet]
[Route("{Query}/{Version}")]
public ActionResult Index(string Query, int Version)
{
...
}
GameTwo controller
[Route("GameTwo")]
public ActionResult Index()
{
return View();
}
routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
}
Try this above
Home Controller
[HttpGet]
public ActionResult serchsomething(string Query)
{
//do something
}
Game Two Controller
public ActionResult Index()
{
return View();
}
Routing
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/*serchsomething action*/
routes.MapRoute(
name: "Your route name 1",
url: "serchsomething/{Query}",
defaults: new
{
controller = "home",
action = "serchsomething"
}
);
/*GameTwo Controller*/
routes.MapRoute(
name: "Your route name 2",
url: "GameTwo",
defaults: new
{
controller = "GameTwo",
action = "Index"
}
);
/* default*/
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}}
Are you giving correct controller name?. I am just seeing your url as
mysite.com/gametwo
but controller name as GameTwo Pls change it as GameTwo and try again.
I'm not sure how I should ask the question, but I am trying to route a SHA1 encryption to be accepted by the ID.
Example
/Home/Index/Id
/Home/Index/A8-75-93-36-DA-4F-74-E1-E0-6B-78-98-DC-AE-FF-1F-17-CA
Is there a better way to do this?
If you have the default route setup:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
and then a controller action taking an id parameter:
public class HomeController: Controller
{
public ActionResult Index(string id)
{
...
}
}
and navigate to /Home/Index/A8-75-93-36-DA-4F-74-E1-E0-6B-78-98-DC-AE-FF-1F-17-CA the Index action of the Home controller will be invoked and the id parameter will be passed the hash value.