MapRouting default querystring values?

扶醉桌前 提交于 2020-02-08 07:19:05

问题


I have this route map (notice that topicName is ignored):

routes.MapRoute(
                "Topics", // Route name
               "Topic/{topicName}/{action}",
                new { controller = "Topic", action = "AddQuestion" });  

And I want it to defaultly map to this Url: http://localhost:51421/Topic/SomeName/AddQuestion?topicId=1 (or if that's not possible,to this url: http://localhost:51421/Topic/SomeName/AddQuestion/topicId/1)
(which should invoke this action:

public ActionResult AddQuestion(int topicId)
        {
            return View();
        }  

)
But either way I need all this data in the url.
What's the correct way to do that?


回答1:


You could add a default route:

routes.MapRoute(
    "Topics",
    "Topic/{topicName}/{action}/{topicId}",
    new { controller = "Topic", action = "AddQuestion", topicId = "1" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{topicId}",
    new { controller = "Topic", action = "AddQuestion", topicId = "1" }
);


来源:https://stackoverflow.com/questions/6039886/maprouting-default-querystring-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!