1- i am Createnew Project MVC Empty and i add new Controller name Home :
public class Home : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
2- and i am moving in Index and craete right to add view
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_MainLayout.cshtml";
}
<h2>Index</h2>
but were i am debugg my project tell me ASP MVC Route Config - The resource cannot be found error
info : and in RouteConfig i have this method
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 }
);
}
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 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've created an empty webapi project and I would like to add a homepage View. For some reason, it does not work - it always returns authorization access deny, but I've set the attribute [AllowAnonymous] on HomeController.
public class RouteConfig
{
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 }
);
}
}
[AllowAnonymous]
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
}
Can anyone help with this issue please? Thanks
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 have a website developed in dot net 2.0 I need to update that in to MVC framework. How to keep my old URLs same in MVC? For eg. WWW.something.com/Home.aspx and WWW.something.com/About.aspx. I need to keep this URL structure. Please help.
Use MVC routing to make your URLs the same as your old site.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "Home.aspx",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "About",
url: "About.aspx",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Note that in MVC 5 you could use attribute routing as an alternative to putting every route in the shared RegisterRoutes method.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Important: Put this before your default route
// or attribute routing won't work.
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class HomeController : Controller
{
[Route("Home.aspx", Name = "Home")]
public ActionResult Index()
{
return View();
}
[Route("About.aspx", Name = "About")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
}
Another alternative would be just to use the new URL scheme and use the IIS rewrite module to configure 301 redirects from your old URL locations to your new ones so you don't lose any traffic and all of your old hyperlinks will still work.