With ASP.NET MVC, I have code as below and the standard route definition. When I navigate to mysite.com/ExtjsRun I get a 403.14 error but when I go to mysite.com/ExtjsRun/index I get the controller executed.
My question is how to get the route /ExtjsRun to default to my index method.
using System.Web.Mvc;
using WebApp.Models;
namespace WebApp.Controllers
{
public class ExtJsRunController : Controller
{
[MultiTenantControllerAllow("svcc,angu")]
public ActionResult Index()
{
var str = "/ExtjsRun/" + Tenant.Name;
return Redirect(str);
}
}
}
Routing:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
namespaces: new[] {"WebApp.Controllers"},
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
Change the Routing code as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
namespaces: new[] {"WebApp.Controllers"},
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ExtJsRun",
action = "Index",
id = UrlParameter.Optional }
);
You are not set the default controller to ExtJsRun and action to Index. By your code, the iis server searches for the Home controller and Index Action in that controller, that's the reason for your access denial
Hope this helps
Related
I can't get a catch all route to work in MVC. I have tried to implement what is shown in this question, but it does not work. I have a controller called OnlineController with an Index action. My RouteConfig.cs is set up like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);
routes.MapRoute("RouteName", "{*url}", new { controller =
"Online", action = "Index" });
}
Browsing to the following URL
Blockquote http://mysite/online/something
gives a 404. Why is it not caught and redirected to the index action on the Online controller?
If you want to catch all routes you should delete "Default" route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
AreaRegistration.RegisterAllAreas();
routes.MapRoute("RouteName", "{*url}", new { controller = "Online", action = "Index" });
}
Routes are chosen in the order they were mapped and your url matches "Default" route.
If you want to catch some routes for existing controllers and actions you can use your "Default" route, but with constraints:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = "Home|Home2", action = "Index|Index2"}
);
routes.MapRoute("RouteName", "{*url}", new { controller =
"Online", action = "Index" });
}
Now, if you controller name is "Home" or "Home2" and action name is "Index" or "Index2" these routes will be handled by existing controllers and actions, if other names - by "Online" controller
I would like to create a dynamic routing to a URL like following:
http://localhost:51577/Item/AnyActionName/Id
Please note that the controller name is static and doesn't need to be dynamic. On the other hand, I need to have the action name part dynamic so that whatever is written in that part of URL, I would redirect the user to the Index action inside of Item controller.
What I have tried so far is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Items",
"Item/{action}/{id}",
new { controller = "Item", action = "Index", id = UrlParameter.Optional });
}
And when I build my app I get a following error:
The resource cannot be found.
Edit:
Here is my Global.asax file and the routeconfig.cs file:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
And here's the content of the RouteConfig.cs file with the answer that #Nkosi provided:
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 }
);
routes.MapRoute(
name: "Items",
url: "Item/{id}/{*slug}",
defaults: new { controller = "Item", action = "Index", slug = UrlParameter.Optional }
);
}
}
What you are referring to in your question is called a slug.
I answered a similar question here for web api
Web api - how to route using slugs?
With the slug at the end the route config would look like this
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Items",
url: "Item/{id}/{*slug}",
defaults: new { controller = "Item", action = "Index", slug = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
which could match an example controller action...
public class ItemController : Controller {
public ActionResult Index(int id, string slug = null) {
//...
}
}
the example URL...
"Item/31223512/Any-Item-Name"
would then have the parameters matched as follows...
id = 31223512
slug = "Any-Item-Name"
And because the slug is optional, the above URL will still be matched to
"Item/31223512"
I have a controller name Dashboard and inside that controller i have an action AdminDashboard . Now by default url of this action becomes /Dashboard/AdminDashboard . I want to map this action to this url /SupervisorDashboard
This is what i am doing but its saying not found
routes.MapRoute(
name: "SupervisorDashboard",
url: "SupervisorDashboard",
defaults: new { controller = "Dashboard", action = "AdminDashboard" }
);
and also how can i redirect to this page using Url.Action
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SupervisorDashboard",
url: "SupervisorDashboard",
defaults: new { controller = "Dashboard", action = "AdminDashboard" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Have you placed this new route definition before default route? Routes are evaluated in the same order in which they were registered. If you put default route before any of custom routes, it will be used (and since you probably don't have any SupervisorDashboardController in code, 404 will be returned).
Url.Action should work correctly, if routes are defined in correct order.
So, for this case, RouteConfig should look like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// this one match /SupervisorDashboard only
routes.MapRoute(
name: "SupervisorDashboard",
url: "SupervisorDashboard",
defaults: new { controller = "Dashboard", action = "AdminDashboard" }
);
// should be last, after any custom route definition
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
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.
I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.