简体   繁体   中英

MVC querystring parameter not passing through to action

I have the following action:

public ActionResult CatchAll(string pathname, bool isPreview)
{
    CatchAllModel model = _aliasModelBuilder.BuildCatchAllModel(pathname, isPreview);

    if (model.Page != null)
    {
        return View(model);
    }
    else
    {
        throw new HttpException(404, "Page not found");
    }
}

And the route for this is

routes.MapRoute(
    name: "Default",
    url: "{*pathname}",
    defaults: new { controller = "Alias", action = "CatchAll", isPreview = false });

Now if I browse to localhost/about-us?isPreview=true , the pathname comes through as about-us but isPreview comes through as false.

Is there anything I'm doing wrong - I thought the route default should be overwritten by the query string

Ok this looks as if the default parameter set in the route is not being overwritten by the querystring. So I removed it from the route:

routes.MapRoute(
    name: "Default",
    url: "{*pathname}",
    defaults: new { controller = "Alias", action = "CatchAll" });

And added it to the action:

public ActionResult CatchAll(string pathname, bool isPreview = false)

I guess I'll have to suppress CA1026 as we can't make overload methods for actions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM