An MVC 6 route does not perform as expected

51 views Asked by At

I have this route:

routes.MapRoute(name: "Trads",
    url: "test_3000/{action}/{traditional}",
    defaults: new { controller = "Test_3000", action = "Subset", traditional = UrlParameter.Optional });

And a Test_3000Controller with this method:

    // GET: Test_3000/Subset?traditional=(Chinese Character)
public ActionResult Subset(string traditional)
{
    if (traditional == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Test_3000 test_3000 = db.Test_3000.Find(traditional);
    if (test_3000 == null)
    {
        return HttpNotFound();
    }
    return View(test_3000);
}

This URL works:

server/test_3000/subset?traditional=的

This URL does NOT work:

server/test_3000/subset/的

In the latter case, 'traditional' is null.

"Traditional" is a column in an SQL table.

2

There are 2 answers

3
Daveo On

Have you tried this

Routes.MapRoute(name: "Trads",
    url: "test_3000/{action}/{traditional?}",
    defaults: new { controller = "Test_3000", action = "Subset", traditional = UrlParameter.Optional });

Notice the ? on traditional. Also

public ActionResult Subset(string traditional  = null)
{ ... }

So that traditional is explicitly set as an optional

0
Jim Kay On
routes.MapRoute(
            name: "ChBoPinCritCji",
            url: "charbopopincrits/subset/{Char}",
            defaults: new { controller = "CharBopoPinCrits", action = "Subset", Char = typeof(string) }
        );

This MapRoute, which is first, now works.

Thanks for the attention.