I was wandering if there is option to do the following
If I call
"admin/Category" - to call "CategoryAdminController"
If I call
"Category" - to call "CategoryController"
It is very easy to do this via routing and custom controller factory. Here is the solution:
// add route
routes.Add(new Route("{culture}/admin/{controller}/{action}/{*id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "", culture = LocalizationManager.DefaultCulture.Name, controllerSufix = "Admin" }),
Constraints = new RouteValueDictionary(new { culture = new CultureRouteConstraint() })
});
Than create custom controller factory
public class CmsControllerFactory : DefaultControllerFactory
{
RequestContext _requestContext;
protected override Type GetControllerType(string controllerName)
{
if (_requestContext.RouteData.Values.ContainsKey("controllerSufix"))
{
string sufix = (string)_requestContext.RouteData.Values["controllerSufix"];
Type type = base.GetControllerType(String.Concat(controllerName, sufix));
if (type != null)
return type;
}
return base.GetControllerType(controllerName);
}
public override IController CreateController(RequestContext requestContext, string controllerName)
{
_requestContext = requestContext;
return base.CreateController(requestContext, controllerName);
}
}
I would like if anybody know some different/better solution.
You can do this quite simply with two route handlers:
routes.MapRoute(
"Admin",
"/admin/category/{id}",
new { controller = "CategoryAdminController", action = "Index", id = "" }
);
and then:
routes.MapRoute(
"Standard",
"/category/{id}",
new { controller = "CategoryController", action = "Index", id = "" }
);
Related
I am calling:
#Html.ActionLink("Register", "Register", "Account", new {area="FrontEnd"})
expecting
www.example.com/Account/Register
but I'm getting
http://www.example.com/?action=Register&controller=Account&area=FrontEnd
I'm a little confused as to why this happening, what can I do to resolve this?
Edit
When I remove the area parameter at the end, it builds the link correctly but to my admin section instead of FrontEnd.
Edit
I've isolated it to a custom route I have setup:
routes.Add("CategoriesRoute", new CategoriesRoute()); //This is breaking ActionLink.
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", area = "FrontEnd", id = UrlParameter.Optional },
new[] { "CC.Web.Areas.FrontEnd.Controllers" }
);
This is the custom route.
public class CategoriesRoute : Route
{
public CategoriesRoute()
: base("{*categories}", new MvcRouteHandler())
{
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var rd = base.GetRouteData(httpContext);
if (rd == null)
return null;
string categories = rd.Values["categories"] as string;
if (categories.IsEmpty())
return null;
string[] parts = categories.Split('/');
if (not a category) //pseudo code
return null;
rd.Values["controller"] = "Category";
rd.Values["action"] = "Index";
rd.Values["area"] = "FrontEnd";
rd.DataTokens.Add("namespaces", new string[] { "CC.Web.Areas.FrontEnd.Controllers" });
rd.Values["categoryString"] = "/" + categories; //Required to get exact match on the cache.
return rd;
}
}
Try the below. I think you're missing an attribute null at the end
#Html.ActionLink("Register", "Register", "Account", new {area="FrontEnd"}, null)
You need to add null for the 5th parameter (html attributes)
#Html.ActionLink("Register", "Register", "Account", new {area="FrontEnd"}, null)
I have a problem, my url is ugly smth like this:
http://localhost:43547/Admin?id=1&str=wooh
I want see the url like this one
http://localhost:43547/LOL
My routes are:
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = UrlParameter.Optional, str = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" }
);
and I have 2 identical views one of them is with such method
#Html.ActionLink("Click me", "Index", "Admin", new { id = 1, str = "wooh" }, null)
which moves the page...
and I have 2 controllers:
public ActionResult Index(int id = 5485, string str = "Default Value")
{
ViewBag.ID = id;
ViewBag.STR = str;
return View();
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Controller = "Home";
ViewBag.Action = "Index";
return View();
}
}
So what's wrong? When I type smth like /LOL
It moves me to another page with default values of id and str...
Change your MyRoute definition to include the id and str parameter values.
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = 1, str = "wooh" }
);
That way your url will look like /LOL but will pass the values you need.
string actionName = "Users";
[HttpGet]
[ActionName(actionName)]
public ActionResult GetMe()
{
}
...gives: An object reference is required for the non-static field, method, or property
That was just a test though, is there a way to do this? If so, I could re-use the same Controller and possibly create new URIs on the fly... right?
Assuming you have the following controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
you could write a custom route handler:
public class MyRouteHander : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var rd = requestContext.RouteData;
var action = rd.GetRequiredString("action");
var controller = rd.GetRequiredString("controller");
if (string.Equals(action, "users", StringComparison.OrdinalIgnoreCase) &&
string.Equals(controller, "home", StringComparison.OrdinalIgnoreCase))
{
// The action name is dynamic
string actionName = "Index";
requestContext.RouteData.Values["action"] = actionName;
}
return new MvcHandler(requestContext);
}
}
Finally associate the custom route handler in your route definitions:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).RouteHandler = new MyRouteHander();
Now if you request /home/users it's the Index action of the Home controller that will be served.
You can just take another routing argument in and do a switch statement.
My route looks like:
routes.Add(new Route("{companyName}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "CompanyController", action = "Index", id = 1 }),
}
);
my action:
public ActionResult Index(string companyName, string id)
{
Response.Write(companyName);
Response.End();
return ViePage("~/views/company/index.aspx");
}
try this:
routes.Add(new Route("{companyName}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Company", action = "Index", id = 1 }),
}
);
when referencing your controllers you don't want to have the "controller" part of the name there.
I would like to create dynamic urls that route to controller actions with an Id value. I've created the following route using a catch-all parameter
routes.MapRoute(
"RouteName",
"{id}/{*Url}",
new { controller = "Controller", action = "Action", id = "" }
);
This works as expected and allows me to use the following Urls:
"http://website.com/1/fake/url/path" (1 being the id that gets passed to the action method)
Does anyone know a way to achieve it this way instead without creating my own http module?:
"http://website.com/fake/url/path/1"
Thanks - Mark
That's a really difficult one, for me anyway.
Given the following route:
routes.MapRoute("Default", "{*token}",
new { controller = "Home", action = "Index", token = 0 });
Your controller and supporting classes would be something like this:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index([ModelBinder(typeof(IndexReqquestBinder))] IndexRequest request)
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = String.Format("We're looking at ID: {0}", request.ID);
return View();
}
}
public class IndexRequest
{
public Int32 ID { get; set; }
public IndexRequest(Int32 id)
{
this.ID = id;
}
}
public class IndexReqquestBinder : IModelBinder
{
public ModelBinderResult BindModel(ModelBindingContext bindingContext)
{
if ( null != bindingContext.RouteData.Values["token"] ) {
foreach ( String v in bindingContext.RouteData.Values["token"].ToString().Split('/') ) {
Int32 id = 0;
if ( Int32.TryParse(v, out id) ) {
return new ModelBinderResult(new IndexRequest(id));
}
}
}
return new ModelBinderResult(new IndexRequest(0));
}
}
Routes redirection is based on route entry in the route table. It must be in systematic way. For instance if you have route as "customurl/id" after {controller}/{action}/{id}(default of mvc) when you will enter "customurl" in the url box it will take it as default route and no page found exception will occur. So if you wan to use custom route then first remove the default route. I do this like.
RouteCollection routes = RouteTable.Routes;
if (routes["rname"] != null)
{
RouteTable.Routes.Remove(routes["rname"]);
}
routes.Remove(routes["Default"]);
routes.MapRoute(
name: "newname",
url: url + "/{customId}",
defaults: new { controller = "Search", action = "Index", customId = UrlParameter.Optional }
);
//default route value
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would suggest overriding of DefaultControllerFactory
> public class CustomControllerFactory : DefaultControllerFactory
> {
> public override IController
> CreateController(System.Web.Routing.RequestContext
> requestContext, string controllerName)
> {
> try
> {
> return base.CreateController(requestContext,
> controllerName);
> }
> catch (Exception exception)
> {
> // collect route data
> string id = (string)requestContext.RouteData.Values["id"];
> string action = (string)requestContext.RouteData.Values["action"];
> // set full path as routing "page" parameter
> VirtualPathData path = requestContext.RouteData.Route.GetVirtualPath(requestContext,
> requestContext.RouteData.Values);
> requestContext.RouteData.Values["id"]
> = path.VirtualPath; // or anything you need
> // use controller page by default
> controllerName = "MyController";
> // set basuc routing data
> requestContext.RouteData.Values["controller"]
> = controllerName;
> requestContext.RouteData.Values["action"]
> = "index";
> // call base method to create controller
> return base.CreateController(requestContext,
> controllerName);
> }
> }
>}
And than just register this as default controller factory in global.asax.cs file
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new MyNameSpace.CustomControllerFactory ());
RegisterRoutes(RouteTable.Routes); // this already exists by default
}