-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.InvalidOperationException : Unable to generate redirect url - after applied custom culture constraint in register route #1
Comments
This is very late, I know, however I want to address this issue anyway:
I believe that your particular scenario expects that a user visiting In your case, because the preferred route changes conditionally (only if the culture is 'nl'), we would need to create a more tailored solution. We could create a See: https://github.com/dipunm/SEO.DuplicateContent/blob/b8f339988b72e0a4926f483c28cef74c0540d67f/ReturnNull.CanonicalRoutes/Rules/EnforceCorrectRoute.cs for an example of a constraint that changes the preferred route. |
Here is an example rule that I created: using ReturnNull.CanonicalRoutes.Models;
using ReturnNull.CanonicalRoutes.Rules.Abstract;
using System.Web.Routing;
using System;
namespace Demo.Web.App_Start
{
internal class EnableFallbackRoute : ISeoRequestRule
{
private string matchedRoute;
private string fallbackRoute;
private bool forceRedirect;
public EnableFallbackRoute(string matchedRoute, string fallbackRoute, bool forceRedirect = true)
{
this.matchedRoute = matchedRoute;
this.forceRedirect = forceRedirect;
this.fallbackRoute = fallbackRoute;
}
public bool HasBeenViolated(RequestData requestData, UserProvisions provisions)
{
/*
* This rule must be placed in the RedirectRules to ensure that
* generating a url to redirect to can happen properly.
*
* If this rule is expected to be a re-write only rule, we can prevent
* the HasBeenViolated method from flagging any issues.
*
* - If another redirect rule has been violated, the CorrectPlan
* method will ensure that the Route used to generate the redirect
* url is compatible.
* - If no other redirect rules have been violated, then the rule
* will not cause any redirects. This allows the rewrite rules to
* be applied instead.
* - If we are not forcing a redirect, we should add another instance of
* this rule in the rewrite rules collection to ensure that the Route
* used to generate the cannonical url is compatible.
*/
if (!forceRedirect)
return false;
return this.RequestUsesIncompatibleRoute(requestData, provisions);
}
private bool RequestUsesIncompatibleRoute(RequestData requestData, UserProvisions provisions)
{
var expectedRoute = RouteTable.Routes[this.matchedRoute];
return expectedRoute == requestData.Route && requestData.Route.GetVirtualPath(
requestData.HttpContext.Request.RequestContext,
requestData.RouteValues) == null;
}
public void CorrectPlan(UrlPlan plan, RequestData requestData, UserProvisions provisions)
{
if (this.RequestUsesIncompatibleRoute(requestData, provisions))
{
plan.Route = RouteTable.Routes[this.fallbackRoute];
}
}
}
} You can add this rule to the ruleset collection like this: public static void SetupDuplicateContentRules(SeoRequestRulesetCollection rules)
{
var defaultRuleset = SeoRequestRuleset.Recommended();
defaultRuleset.RedirectRules.Add(new EnableFallbackRoute("DefaultWithCulture", "Default", forceRedirect: false));
defaultRuleset.RewriteRules.Add(new EnableFallbackRoute("DefaultWithCulture", "Default"));
rules.Add("Default", defaultRuleset);
} If you DO want the library to redirect, then you can omit the RewriteRule part, but public static void SetupDuplicateContentRules(SeoRequestRulesetCollection rules)
{
var defaultRuleset = SeoRequestRuleset.Recommended();
defaultRuleset.RedirectRules.Add(new EnableFallbackRoute("DefaultWithCulture", "Default", forceRedirect: true));
rules.Add("Default", defaultRuleset);
} |
I am applying custom route constraints suggested by NightOwl888
http://stackoverflow.com/questions/32764989/asp-net-mvc-5-culture-in-route-and-url
public bool Match( HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (routeDirection == RouteDirection.UrlGeneration && this.defaultCulture.Equals(values[parameterName])) { return false; } else { return Regex.IsMatch((string)values[parameterName], "^" + pattern + "$"); } }
when it match the condition in
it will throw Unable to generate redirect url exception due to the first match return false and it suppose to retrieve from next MapRoute
The text was updated successfully, but these errors were encountered: