I need urls like /controller/verb/noun/id and my action methods would be verb+noun. For example I want /home/edit/team/3 to hit the action method
public ActionResult editteam(int id){}
I have following route in my global.asax file.
routes.MapRoute(
"test",
"{controller}.mvc/{verb}/{noun}/{id}",
new { docid = "", action = "{verb}"+"{noun}", id = "" }
);
URLs correctly match the route but I don't know where should I construct the action parameter that is name of action method being called.
Try:
public class VerbNounRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
IRouteHandler handler = new MvcRouteHandler();
var vals = requestContext.RouteData.Values;
vals["action"] = (string)vals["verb"] + vals["noun"];
return handler.GetHttpHandler(requestContext);
}
}
I don't know of a way to hook it for all routes automatically, but in your case you can do it for that entry like:
routes.MapRoute(
"test",
"{controller}.mvc/{verb}/{noun}/{id}",
new { docid = "", id = "" }
).RouteHandler = new VerbNounRouteHandler();
Try the following, use this as your route:
routes.MapRoute(
"HomeVerbNounRoute",
"{controller}/{verb}/{noun}/{id}",
new { controller = "Home", action = "{verb}"+"{noun}", id = UrlParameter.Optional }
);
Then you simply put your actionresult method:
public ActionResult editteam(int id){}
Into Controllers/HomeController.cs and that will get called when a URL matches the provided route.
Related
I've a route like Admin/Vendor in my MVC application . Without changing this route I need to point this same route to another method say CustomAdmin/CustomVendor.
I tried attribute routing but no luck . Is there any way to do this. My current code is given below
Original Method:
public class AdminController
{
public ActionResult Vendor()
{
return View();
}
}
Custom Method:
public class CustomAdminController
{
[Route("Admin/Vendor")]
public ActionResult CustomVendor()
{
return View();
}
}
As you're developing a plugin. You have to add your custom route to the RouteProvider.
In default nopCommerce AdminController and Vendor doesn't exists, so I assume that you're trying to override vendor list method of admin.
Which looks like:
public partial class RouteProvider : IRouteProvider
{
public void RegisterRoutes(RouteCollection routes)
{
var route = routes.MapRoute("Plugin.GroupName.PluginName.CustomVendor",
"Admin/Vendor/List",
new { controller = "CustomAdminController", action = "CustomVendor", orderIds = UrlParameter.Optional, area = "Admin" },
new[] { "Nop.Plugin.GroupName.PluginName.Controllers" });
route.DataTokens.Add("area", "admin");
routes.Remove(route);
routes.Insert(0, route);
}
public int Priority
{
get
{
return 100; // route priority
}
}
}
Side Note: GroupName and PluginName should be your plugin group name and plugin name.
Hope this helps !
On your plugin which class implements the interface IRouteProvider, you can easily override the route there.
Likewise I have a class named RouteProvider in my plugin, So I have Implemented the abstract function RegisterRoutes and simply it can be overrided by
routes.MapRoute("Plugin.Promotion.Combo.SaveGeneralSettings",
"Admin/Vendor",
new { controller = "CustomAdmin", action = "CustomVendor", },
new[] { "Nop.Plugin.Promotion.Combo.Controllers" }
);
Here Plugin.Promotion.Combo must be replaced by your plugin directory.And using SaveGeneralSettings or any things you want to use that will be your route url
I am trying to download a file, when I click on a hyperlink, but I always get a Resource not found exception.
The controller action never gets hit.
Index.cs.html:
#{
ViewBag.Title = "Files";
}
#Html.ActionLink("Download Report", "DownloadFile", "FileDownload", new { type = "pdf" }, new { #class = "" })
Rendered Action-Link:
http://localhost:58255/FileDownload/DownloadFile?type=pdf
When I uncomment the MapRoute entry "Files" in the routeConfig the action link is rendered like this:
http://localhost:58255/FileDownload/DownloadFile/pdf
But neither of them work! -> The resource cannot be found
Controller (Only Index action gets hit when the view loads, but nothing when I click on the actionlink):
[AllowAnonymous]
public class FileDownloadController : Controller
{
public ActionResult Index()
{
return View();
}
FilePathResult DownloadFile(string type)
{
var fn = $"~/Documents/Test.{type}";
return File(fn, "application/pdf", Server.UrlEncode(fn));
}
RegisterRoutes.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes(); // for attribute routing on action
AreaRegistration.RegisterAllAreas();
//routes.MapRoute(
// name: "Files",
// url: "{controller}/{action}/{type}",
// defaults: new { controller = "FileDownload", action = "DownloadFile", type = UrlParameter.Optional }
//);
// Should be last
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
...coming from the WebForms area...
Please try to add public access modifier before action name
public FilePathResult DownloadFile(string type)
{
var fn = $"~/Documents/Test.{type}";
return File(fn, "application/pdf", Server.UrlEncode(fn));
}
My project is multilingual and also has routing, but I need to allow hyphens in the URLs, such as: example.com/en/our-team.
In this website (http://www.bousie.co.uk/blog/allow-dashes-within-urls-using-asp-net-mvc4/) I found a solution to this, adding the following code in Global.asax :
public class HyphenatedRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["controller"] =
requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
requestContext.RouteData.Values["action"] =
requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
return base.GetHttpHandler(requestContext);
}
}
And on RouteConfig.cs, replace this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
With this:
routes.Add(
new Route("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Home", action = "Index", id = "" }),
new HyphenatedRouteHandler())
);
The problem is that i don't know how to do it in my code:
using System.Web.Mvc;
using System.Web.Routing;
namespace theme_mvc
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapLocalizeRoute("Default",
url: "{culture}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = "[a-zA-Z]{1}[a-zA-Z]{1}" });
routes.MapRouteToLocalizeRedirect("RedirectToLocalize",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
}
}
How could i adapt my code to support hyphens (-) in the URL?
The link you have posted to uses the wrong approach. Routing is a 2-way mapping, but if you only customize the MvcRouteHandler, you are only taking into account the incoming routes.
This issue comes up so often that someone has created a hyphenated lowercase route project on GitHub, which would do what you want except you need to also combine it with the route that is being called by your MapLocalizeRoute extension method.
But since you have not posted the code for MapLocalizeRoute or MapRouteToLocalizeRedirect I can't tell you how that is done.
I have two routes mapped in my MVC application:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Project",
"{controller}/{projectid}/{action}/{id}",
new { controller = "Project", action = "Index", id = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
For the most part, these pose no problems, but I do strike trouble when trying to call the /Account/LogOn view. My ActionLink syntax looks like this (I've tried a couple of variations including null route values and such):
<%= Html.ActionLink("Log On", "LogOn", "Account") %>
Browsing to this view raises a 404 error ("The resource cannot be found"). Ideally I'd like to avoid a huge amount of rework, so what would be the best way to avoid these clashes?
Have you tried this variation with the literal for the controller?
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Project",
"Project/{projectid}/{action}/{id}",
new { controller = "Project", action = "Index", id = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
This would catch: http://localhost/Project/12 and http://localhost/Project/12/Edit and http://localhost/Project/2/View/2
But it would pass http://localhost/Account/LogOn to the second rounte. Yes?
I am working on SportsStore example on chapter 4 from the following book and getting stuck...
Pro Asp.net mvc framework
I get the following error:
The current request for action 'List' on controller type 'ProductsController' is ambiguous between the following action methods:
System.Web.Mvc.ViewResult List() on type WebUI.Controllers.ProductsController
System.Web.Mvc.ViewResult List(Int32) on type WebUI.Controllers.ProductsController ..
My router code looks as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
null, // Route name
"", // URL with parameters
new { controller = "Products", action = "List", page=1 }
);
routes.MapRoute(
null, // Route name
"Page{page}", // URL with parameters
new { controller = "Products", action = "List" }, // Parameter defaults
new { page = #"\d+" }
);
}
and controller code looks as follows:
public ViewResult List()
{
return View(productsRepository.Products.ToList());
}
public ViewResult List(int page)
{
return View(productsRepository.Products
.Skip((page - 1) * PageSize)
.Take(PageSize)
.ToList());
}
What am I missing?
my url is as follows:
http://localhost:1103/
or
http://localhost:1103/Page1
or http://localhost:1103/Page2
thanks
Remove the List() controller method. In the book, it states that you needed to update the existing List() method to the the one you currently have List(int page).