ZombieBunnies
ZombieBunnies

Reputation: 268

Route String Added On Redirect.Response But Not Assigned To [RouteData] string

I have a button that uses a code behind method to do stuff. After finishing I have a Redirect.Response that should forward the user and use the route data string to get all products matching it.

Instead it is forwarding the user to the destination URL properly ([domain]/ScreeningDateTime/a38eceeb-5753-4156-ae2d-6a74cf7fef1e-64722426), but displaying all results from that table, instead of just results matching the RouteData string.

I wanted to note a few things: I can enter [domain]/ScreeningDateTime?id=1 and have it work properly. In debug (VS2013.3Pro) if I pin the InternalId it displays the string "a38eceeb-5753-4156-ae2d-6a74cf7fef1e-64722426" properly on the initial page. On the ScreeningDateTime page, it does not.

Code with some stuff removed for brevity (I have tested it as it is displayed with the same results):

From the initial page

Response.Redirect(
            GetRouteUrl("ScreeningsByDateTimeRoute", new { internalId = InternalId }));

From the Global.asax

    void RegisterCustomRoutes(RouteCollection routes)
    {
        routes.MapPageRoute(
        "ScreeningsByDateTimeRoute",
        "ScreeningDateTime/{internalId}",
        "~/ScreeningDateTime.aspx"
        );

        *More Routes*

From the ScreeningDateTime code behind (Destination Page):

    public IQueryable<Screening> GetProducts(
        [QueryString("id")] int? id,
        [RouteData] string internalId)
    {
        var _db = new ProductContext();
        IQueryable<Screening> query = _db.Screenings;

        if (id.HasValue && id>0)
        {
            query = query.Where(p => p.ScreeningID == id);
        }

        if (!String.IsNullOrEmpty(internalId))
        {
            query = query.Where(p =>String.Compare(p.InternalId,internalId) == 0);
        }
        return query;
    }

If have looked for a fix for longer than I'd like to admit. Any help would be greatly appreciated. :)

Thanks

Upvotes: 0

Views: 567

Answers (1)

MikeSmithDev
MikeSmithDev

Reputation: 15797

I've always accessed route variables in .aspx.cs as Page.RouteData.Values[variableName]. Try this code:

public IQueryable<Screening> GetProducts()
{
    var _db = new ProductContext();
    IQueryable<Screening> query = _db.Screenings;

    if (!String.IsNullOrEmpty(Request.QueryString["id"]))
    {
        query = query.Where(p => p.ScreeningID == Request.QueryString["id"]);
    }
    else if (Page.RouteData.Values["internalId"] != null && !String.IsNullOrEmpty(Page.RouteData.Values["internalId"] as string))
    {
        query = query.Where(p => p.InternalId == Page.RouteData.Values["internalId"].ToString());
    }
    return query;
}

UPDATE:

I'm wondering if you are using something like FriendlyURLs or URL rewriting and that is causing a conflict between /ScreeningDateTime.aspx and /ScreeningDateTime/. Try rename the route URL to something else, like:

routes.MapPageRoute(
        "ScreeningsByDateTimeRoute",
        "ScreeningDateTimeIntId/{internalId}",
        "~/ScreeningDateTime.aspx"
        );

Upvotes: 0

Related Questions