How do you Route a page to an Attribute Route in M

2019-09-10 10:14发布

I'm in the process of migrating some of our legacy aspx pages to a shiny new MVC based architecture. However, during the transition, I still need to support the old routes, and just either redirect, or render the relevant mvc action.

The issue I have is that 1 aspx that used to handle multiple purposes is now multiple MVC actions.

I figured that I could use a custom RouteBase with:

        routes.Add("legacy", new LegacyPageOverride());

That looks like:

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        if (httpContext.Request.Headers == null)
            return null;

        if (httpContext.Request.Url == null)
            return null;

        if (httpContext.Request.Url.AbsolutePath.StartsWith("/legacy/login/default.aspx", StringComparison.OrdinalIgnoreCase))
            return LoginRoute(httpContext.Request);

        return null;
    }

    private RouteData LoginRoute(HttpRequestBase httpRequestBase)
    {
        if (httpRequestBase?.Url == null)
            throw new ArgumentNullException(nameof(httpRequestBase));

        var routeData = new RouteData(this, new MvcRouteHandler());
        routeData.Values.Add("controller", "Account");

        if (httpRequestBase.Form["SAMLResponse"] != null || 
            httpRequestBase.QueryString["SAMLResponse"] != null)
        {
            routeData.Values.Add("action", "SAML");
        }
        else if (httpRequestBase.QueryString["logged_out"] != null)
        {
            routeData.Values.Add("action", "logoff");
        }
        else
        {
            routeData.Values.Add("action", "SignIn");
        }
        return routeData;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        return null;
    }

I've debugged it through, but although this logic is firing, and GetRouteData is returning a RouteData object, I still receive a 404.

I've tried:

routes.RouteExistingFiles = true (and false).

Deleting the files I'm trying to override the route for.

Trying to remove the staticFile module from the web.config.

I can't use UrlRewriting as I can't get the module installed on all the webservers that host the site. I also need to route based on POST parameters, and have these re-submitted to the resultant page.

UPDATE:

I've narrowed this down to attribute routing. If I disable that on the specific controller action, everything works fine.

So this doesn't work: [Route("login)] public ActionResult Login()

This does: public ActionResult Login()

Any ideas how to make it work? I read something MS_DirectRouteMatches?

0条回答
登录 后发表回答