I have a website defined in Sitecore's site definitions. The path to it is /localhost/mysite/home. And it works.
I need to create a custom controller to submit forms with an API bypassing Sitecore. So I have FormsController (inheriting from MVC controller) with an action named "Test" taking no parameters.
I defined the route in the initialize pipeline like this:
public class Initialize
{
public void Process(PipelineArgs args)
{
MapRoutes();
GlassMapperSc.Start();
}
private void MapRoutes()
{
RouteTable.Routes.MapRoute(
"Forms.Test",
"forms/test",
new
{
controller = "FormsController",
action = "Test"
},
new[] { "Forms.Controller.Namespace" });
}
}
The route is added to the route table correctly, and it's there when I debug it.
Now, when I try to call method "test", the route is not found and the debugger doesn't hit the breakpoint in the action.
I'm trying different routes:
/localhost/mysite/home/forms/test
/localhost/forms/test (default website)
But no luck so far.
---- UPDATE ---
Going deeper into it, I noticed that there's something wrong with Sitecore's behavior. The TransferRoutedRequest processor is supposed to abort the httpRequestBegin pipeline, giving control back to MVC, in case the context item is null (simplifying). It happens after some checks, among which is one on RoutTable data. But the call to RouteTable.Routes.GetRouteData returns always null, which makes the processor return without aborting the pipeline. I overrode it to make it abort the pipeline correctly, but still, even if I it calls method args.AbortPipeline(), pipeline is not aborted and route not resolved.
this is how the original TransferRoutedRequest looked like:
public class TransferRoutedRequest : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull((object) args, "args");
RouteData routeData = RouteTable.Routes.GetRouteData((HttpContextBase) new HttpContextWrapper(HttpContext.Current));
if (routeData == null)
return;
RouteValueDictionary routeValueDictionary = ObjectExtensions.ValueOrDefault<Route, RouteValueDictionary>(routeData.Route as Route, (Func<Route, RouteValueDictionary>) (r => r.Defaults));
if (routeValueDictionary != null && routeValueDictionary.ContainsKey("scIsFallThrough"))
return;
args.AbortPipeline();
}
}
and this is how I overrode it:
public class TransferRoutedRequest : global::Sitecore.Mvc.Pipelines.HttpRequest.TransferRoutedRequest
{
public override void Process(HttpRequestArgs args)
{
if (Context.Item == null || Context.Item.Visualization.Layout == null)
args.AbortPipeline();
else
base.Process(args);
}
}
Here is a working example taken from one of my projects.
Custom Route Registration:
namespace Test.Project.Pipelines.Initialize
{
public class InitRoutes : Sitecore.Mvc.Pipelines.Loader.InitializeRoutes
{
public override void Process(PipelineArgs args)
{
RegisterRoutes(RouteTable.Routes);
}
protected virtual void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Test", // Route name
"api/test/{controller}/{action}/{id}", // URL with parameters
new { id = UrlParameter.Optional }
);
}
}
}
Initialize Pipeline Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type="Test.Project.Pipelines.Initialize.InitRoutes, Test.Project"
patch:after="processor[#type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
</initialize>
</pipelines>
</sitecore>
</configuration>
Here is the code that will create a route for you. In global.asax.cs you will call RegisterRoutes from App_Start event handler:
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
And there you specify your route as:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "test",
url: "mvc/Forms/{action}/{id}",
defaults: new { controller = "Forms", action = "Test", id = UrlParameter.Optional }
);
}
You will have /mvc/ prefix in this case that will handle your route to specifies controller, so you will call it as:
/mvc/Forms/Test/{you_may_pass_some_optional_GUID_here}
This will route to FormsController class action method Test(string id) but you may omit id parameter
Finally I've got it working correctly. As I wrote TransferRoutedRequest wasn't working as expected, so I had to override it. Still, even if it worked as it's supposed to, the route wasn't being resolved. The problem was in the pipeline configuration. Thanks to a colleague I opened SitecoreRocks pipeline tool and it showed the pipeline was registered in a position too far away from where it should have been, so it was never hit (I had registered it after ItemResolver, as it was on the original configuration). I patched it before LayoutResolver and that did the trick. The route was resolved.
Still, Sitecore wasn't able to create an instance of the type, as it was in another assembly. Even specifying the controller's namespace didn't solve the problem. So I had to make some modifications and override CreateController method of ControllerFactory class and override InitializeControllerFactory processor (which I already had modified to be able to work with a DI container), writing a new ControllerFactory and a new SitecoreControllerFactory.
The final code looks like this:
public override IController CreateController(RequestContext requestContext, string controllerName)
{
var controller = SC.Context.Item == null || SC.Context.Item.Visualization.Layout == null
? base.GetControllerType(requestContext, controllerName)
: TypeHelper.GetType(controllerName);
return GetControllerInstance(requestContext, controller);
}
In case I'm dealing with a Sitecore item, I use TypeHelper to return current controller type from a controller rendering or layout. Otherwise I use DefaultControllerFactory.GetControllerType to resolve the custom route.
The only thing to be careful: in case 2 or more namespaces have a controller with the same name and action, it's mandatory to add a namespace in order to identify them.
Related
i already have one MVC website in english language. Now I need to add arabic language. To do that i want to use structure below to store my localized .cshtml files
English Views
~/Views/index.cshtml
~/Views/about.cshtml
Arabic Views
~/ar/Views/index.cshtml
~/ar/Views/about.cshtml
English URL (It's working)
www.samplesite.com/home/index
www.samplesite.com/home/index
Arabic URL (It's not working)
www.samplesite.com/ar/home/index
www.samplesite.com/ar/home/index
I need to return english view if url with /en or no language parameter and if URL starts with /ar i need to return view in ar/Views folder. What should i change in route config and actions to get the correct view based on URL ?
In order to localize the ASP.NET MVC app with different views for different languages and cultures, you need to at least do:
Save language code from URL to route data collection, if exists
Set current culture based on the language code, if exists
Route the request to localized views, if language code appears
1. Save language code from URL to route data collection, if exists
You need to define the route mapping so that whenever there is a language code on the URL, it would be put to the route data collection.
You can define the extra route mapping in the RouteConfig:
namespace DL.SO.Globalization.DotNet.Web.UI
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "DefaultWithLang",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { lang = new LanguageRouteConstraint() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
We will come back to the route constraint, but the idea here is to run this "DefaultWithLang" mapping first, and see if the incoming request has the language code attached on it or not. If yes, save it to the route collection with the key lang. Otherwise, just process it as normal.
Route constraints
Imagine we have those 2 mappings defined, and the incoming request looks like /home/index. Without that route constraint, this would be mapped to the "DefaultWithLang" with lang being "home", which is incorrect.
We will need a way to test whether the language code is a valid 2-letter code. To do that, you can create a route constraint:
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;
namespace DL.SO.Globalization.DotNet.Web.UI.RouteConstraints
{
public class LanguageRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
return Regex.IsMatch((string)values[parameterName], #"^[a-z]{2}$");
}
}
}
With this in placed, the request "/home/index" will first come in with "home" being the language code, but it will fail this route constraint because it has more than 2 letters. This would result no matching for "DefaultWithLang" map. Then the MVC will continue to use the 2nd mapping and see if the request is a valid request, which it is!
2. Set current culture based on the language code, if exists
Now we know the language code will be put into the route data collection. We can set the current culture and the current UI culture of the current thread based on that.
To do so, we can create an ActionFilter:
namespace DL.SO.Globalization.DotNet.Web.UI.Filters
{
public class LanguageFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var values = filterContext.RouteData.Values;
string languageCode = (string)values["lang"] ?? "en";
var cultureInfo = new CultureInfo(languageCode);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
}
}
}
Here I am reading the language code value from the route data collection, or it defaults to "en" if it doesn't exist.
To enable it for the whole application, you can add it to FilterConfig.cs:
namespace DL.SO.Globalization.DotNet.Web.UI
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new LanguageFilter());
filters.Add(new HandleErrorAttribute());
}
}
}
3. Route the request to localized views, if language code appears
Similar to Step #2, now we have the language code, we need to create a custom view engine to route the request to different views:
namespace DL.SO.Globalization.DotNet.Web.UI
{
public class GlobalizationRazorViewEngine : RazorViewEngine
{
protected override IView CreatePartialView(ControllerContext controllerContext,
string partialPath)
{
partialPath = GetGlobalizeViewPath(controllerContext, partialPath);
return base.CreatePartialView(controllerContext, partialPath);
}
protected override IView CreateView(ControllerContext controllerContext,
string viewPath, string masterPath)
{
viewPath = GetGlobalizeViewPath(controllerContext, viewPath);
return base.CreateView(controllerContext, viewPath, masterPath);
}
private string GetGlobalizeViewPath(ControllerContext controllerContext,
string viewPath)
{
var request = controllerContext.HttpContext.Request;
var values = controllerContext.RouteData.Values;
string languageCode = (string)values["lang"];
if (!String.IsNullOrWhiteSpace(languageCode))
{
string localizedViewPath = Regex.Replace(viewPath,
"^~/Views/",
String.Format("~/{0}/Views/", languageCode)
);
if (File.Exists(request.MapPath(localizedViewPath)))
{
viewPath = localizedViewPath;
}
}
return viewPath;
}
}
}
The logic here should be straight forward: if the language code exists, try to append it to the original view path the MVC app was going to use to find the corresponding view.
Note: you would need to copy _viewStart.cshtml as well as web.config from the original Views folder to the new localized Views folder. You might consider using different folder structure to contain all localized views. One possible way is to put them all inside the ~/Views folder.
To use this custom view engine, you would need to add it to the view engine collection at Application_Start():
namespace DL.SO.Globalization.DotNet.Web.UI
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new GlobalizationRazorViewEngine());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Screenshots
With zh/ on the URL:
The folder structure:
Github source code: https://github.com/davidliang2008/DL.SO.Globalization.DotNet/tree/master/DL.SO.Globalization.DotNet.Web.UI
Technical Information
AngularJS single page app
Umbraco 7.3.0 website, extended to register routes via Umbraco.Core.ApplicationEventHandler in a separate class library
Scenario
I have an AngularJS single page app (SPA) that I'm trying to pre-render via an external PhantomJS service.
I want MVC's route handler to ignore the route /?_escaped_fragment_={fragment}, so the request can be handled directly by ASP.NET and thus passed on to IIS to proxy the request.
In Theory
Umbraco is built on ASP.NET MVC.
Routes are configurable via System.Web.Routing.RouteCollection class.
When extending Umbraco with custom routes, any routes configured via the System.Web.Routing.RouteTable will take precedence over Umbraco routes, thus never being handled by Umbraco**
Possible methods for my scenario
public void Ignore(string url) or
public void Ignore(string url, object constraints)
**I could be wrong. As far as I'm aware, custom routing takes precedence as it's done before the Umbraco routes are registered. However I'm unsure whether telling MVC to ignore a route would also prevent Umbraco from handling that route.
In Practise
I have attempted to ignore the routes with the following:
Attempt one:
routes.Ignore("?_escaped_fragment_={*pathInfo}");
This throws an error: The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Attempt two:
routes.Ignore("{*escapedfragment}", new { escapedfragment = #".*\?_escaped_fragment_=\/(.*)" });
This didn't result in an error, however Umbraco still picked up the request and handed me back my root page. Regex validation on Regexr.
Questions
Can MVC actually ignore a route based on its query string?
Is my knowledge of Umbraco's routing correct?
Is my regex correct?
Or am I missing something?
The built-in routing behavior doesn't take the query string into consideration. However, routing is extensible and can be based on query string if needed.
The simplest solution is to make a custom RouteBase subclass that can detect your query string, and then use the StopRoutingHandler to ensure the route doesn't function.
public class IgnoreQueryStringKeyRoute : RouteBase
{
private readonly string queryStringKey;
public IgnoreQueryStringKeyRoute(string queryStringKey)
{
if (string.IsNullOrWhiteSpace(queryStringKey))
throw new ArgumentNullException("queryStringKey is required");
this.queryStringKey = queryStringKey;
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
if (httpContext.Request.QueryString.AllKeys.Any(x => x == queryStringKey))
{
return new RouteData(this, new StopRoutingHandler());
}
// Tell MVC this route did not match
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
// Tell MVC this route did not match
return null;
}
}
Usage
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// This route should go first
routes.Add(
name: "IgnoreQuery",
item: new IgnoreQueryStringKeyRoute("_escaped_fragment_"));
// Any other routes should be registered after...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Hi I have problem with routes in plugin, in nopcommerce 3.6
I have in folder Controller TestPohodaController.cs contains method ImportProductInfo()
There is my RegisterRoutes:
namespace Nop.Plugin.Test.Pohoda
{
public partial class RouteProvider : IRouteProvider
{
public void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Plugin.Test.Pohoda.ImportProductInfo",
"Plugins/TestPohoda/ImportProductInfo",
new { controller = "TestPohoda", action = "ImportProductInfo" },
new[] { "Nop.Plugin.Test.Pohoda.Controllers" }
);
}
public int Priority
{
get
{
return 0;
}
}
}
}
Installation to nopCommerce is ok, but when I go to mypage/Plugins/TestPohoda/ImportProductInfo page return 404.
I need url of TestPohodaController to call this controller from economic system. Can You help me please? Thanks.
ASP.NET MVC Routing evaluates routes from top to bottom. So if two routes match, the first one it hits (the one closer to the 'top' of the RegisterRoutes method) will take precedence over the subsequent one.
With that in mind, you need to do two things to fix your problem:
Your default route should be at the bottom.
Your routes need to have constraints on them if they contain the same number of segments:
What's the difference between:
example.com/1
and
example.com/index
To the parser, they contain the same number of segments, and there's no differentiator, so it's going to hit the first route in the list that matches.
To fix that, you should make sure the routes that use ProductIds take constraints:
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
new { id = #"\d+" } //one or more digits only, no alphabetical characters
);
You don't need to start with Plugins for your route url. it is enough
to follow this pattern {controller}/{Action}/{parameter}
Make sure also namespace for the controller is correct as you define
in the routing. Nop.Plugin.Test.Pohoda.Controllers
You can define an optional productId parameter as well. so it will
work for mypage/TestPohoda/ImportProductInfo or
mypage/TestPohoda/ImportProductInfo/123
You can also set the priority higher than 0 which is priority of the
default routeprovider in the nop.web. this way you ensure that your
plugin reads it first. Indeed it is not necessary as you have
specific url. this is only required if you have similar route url
Try using this route
namespace Nop.Plugin.Test.Pohoda
{
public partial class RouteProvider : IRouteProvider
{
public void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Plugin.Test.Pohoda.ImportProductInfo",
"TestPohoda/ImportProductInfo/{productId}",
new { controller = "TestPohoda", action = "ImportProductInfo" , productId = = UrlParameter.Optional },
new[] { "Nop.Plugin.Test.Pohoda.Controllers" }
);
}
public int Priority
{
get
{
return 1;
}
}
}
}
We will have a look at how to register plugin routes. ASP.NET routing is responsible for mapping incoming browser requests to particular MVC controller actions. You can find more information about routing here. So follow the next steps:
If you need to add some custom route, then create RouteProvider.cs file. It informs the nopCommerce system about plugin routes. For example, the following RouteProvider class adds a new route which can be accessed by opening your web browser and navigating to http://www.yourStore.com/Plugins/PaymentPayPalStandard/PDTHandler URL (used by PayPal plugin):
public partial class RouteProvider : IRouteProvider
{
public void RegisterRoutes(IRouteBuilder routeBuilder)
{
routeBuilder.MapRoute("Plugin.Payments.PayPalStandard.PDTHandler", "Plugins/PaymentPayPalStandard/PDTHandler",
new { controller = "PaymentPayPalStandard", action = "PDTHandler" });
}
public int Priority
{
get
{
return -1;
}
}
}
It could be cache problem, try to restart IIS
actually you do nota have to register route by default you can call your method
/TestPohoda/ImportProductInfo
I have an api using web api 2 and I am trying to create help docs within an Area so that an incoming request like ...api/people.help will route to the people controller and people view and serve up the html. I am struggling with the route mapping for the area after refactoring the code. Previously, I had routes like this:
public override void RegisterArea(AreaRegistrationContext context) {
context.MapRoute(
name: "Help",
url: "{action}.help",
defaults: new { area = "help", controller = "default" }
);
All the methods were in the default controller and this worked. Now, I need a separate controller for each resource (eg people, schedules etc) but can't get the routes to work. I would appreciate help, I am very new to this. How do I get each help request to map to the controller with the same action name?
Wouldn't it simply be something similar to:
public override RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Help",
url: "api/{controller}.help",
defaults: new { area = "help" }
);
}
What you have in your post is a default for the name of the controller, in this case, it's name will always be default. Instead, what you're looking for is that when someone routes to your controller name suffixed with .help, it'll route to a path akin to api/help/people, which will end up calling a default action (in MVC) such as index.cshtml or the default action for a GET request to the controller (for WebAPI).
So, you want to set the default area to help as shown above. You also want to set the default action that should execute on the provided controller.
Update: To answer question in comment
For MVC, you can have an action method whose name matches what the controller name will be in the URL:
public class PeopleController : Controller
{
[HttpGet] // Not strictly necessary, but just want to stress this is GET
public ActionResult People()
{
// Do stuff in your action method
}
}
The only problem is, your action method will be different for each controller, and so unknowable for route registration purposes. Therefore, you should maybe have just a default Help action:
public class PeopleController : Controller
{
[HttpGet]
public ActionResult Help()
{
// Do stuff
}
}
Then you can have the following route:
public override RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Help",
url: "api/{controller}.help",
defaults: new { area = "help", action = "Help" }
}
You could take this one step further and provide a Help method in a custom base controller class:
public class MyBaseController : Controller
{
public virtual ActionResult Help()
{
// Do default behavior stuff, if appropriate
}
// If you don't have any "default" behavior, you could make the method abstract:
// public abstract ActionResult Help();
}
public class PeopleController : MyBaseController
{
public override ActionResult Help()
{
// Do stuff.
}
}
Update to further answer OP's question in comments
So, now the OP is saying: "but I want my view to have the same name as my controller." Ok, that should be no problem:
public class PeopleController : MyBaseController // if you're using a base class
{
public override ActionResult Help()
{
return ViewResult("People");
}
}
So, you can have a view with any name you want. But if the view's name differs from the name of the action method, then when returning (say) a ViewResult, you'll need to specify the name of the view to return.
Having said all that, the default folder structure for views in ASP.Net is Areas/{AreaName}/Views/{Controller}/{viewname}.{cs|vb}html. And here, {viewname} is by default assumed to be the action method name, but doesn't have to be when, as above, explicitly telling MVC which view to return (in the example above, People.cshtml).
HTH.
I am working on building an MVC frontend for a CMS system. The CMS system will serve ASP.NET MVC with pages and content.
In Global.asax I registered a custom route handler like this:
public class MvcApplication : EPiServer.Global
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("{*data}", new MvcRouteHandler()));
}
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new MvcControllerFactory());
ModelBinders.Binders.Add(typeof(MvcPageData), new PageDataModelBinder());
RegisterRoutes(RouteTable.Routes);
}
}
This is how my route handler looks like:
public class MvcRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcRequestHandler(requestContext);
}
}
And my MVC request handler:
protected void ProcessRequest(HttpContext httpContext)
{
List<string> pageParams;
// Get the requested page from the CMS
EPiServer.UrlBuilder internalUrlBuilder = GetInternalUrl(httpContext.Request.RawUrl, out pageParams);
MvcPageData mvcPage =
CurrentPageResolver.Instance.GetCurrentPage(internalUrlBuilder.QueryCollection["id"] ??
string.Empty);
string controllerName = mvcPage.ControllerName;
if (pageParams.Count == 0)
{
mvcHandler.RequestContext.RouteData.Values.Add("action", "Index");
}
else
{
mvcHandler.RequestContext.RouteData.Values.Add("action", pageParams[0]);
}
mvcHandler.RequestContext.RouteData.Values.Add("controller", controllerName);
mvcHandler.RequestContext.RouteData.Values["data"] = mvcPage; // This works fine, but I also want to add the remidning pageParams
IController controller = ControllerBuilder.Current.
GetControllerFactory().CreateController(mvcHandler.RequestContext, controllerName);
controller.Execute(mvcHandler.RequestContext);
}
This is what a controller looks like today:
public class StandardController : Controller
{
public ActionResult Index(MvcPageData currentPage)
{
return View(currentPage);
}
}
My problem is that I want to be able to pass more than one parameter to the controller, so I will need to change the mvcHandler.RequestContext.RouteData.Values["data"] to contain some kind of list of parameters. I have searched but not found an solution to the problem, maybe it is really simple. The resulting controler might look something like this:
public ActionResult Index(MvcPageData currentPage, int id, string name)
Anyone knows how to do this? Thanks.
You can try to create array of objects with items count pageParams.Count and add to it the mvcPage object and all objects from pageParams except the first one (which is your action name).
I had missunderstood how the model binding works in MVC. I thought all the data sent to the controller had to be defined in ProcessRequest before calling controller.Execute, which not is the case.
Have you tried registering the handler in the Web.config?
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="yourApiUrlPrefix/*"
type="MvcRouteHandler, YourAssemblyName" />
</httpHandlers>
</system.web>
</configuration>