所以我在MVC
根據什麼其他職位提到閱讀routes
和配置。所以我的目的是要產生如下網址:添加額外的數據作爲URL的一部分,而不是作爲參數
http://domain.com/controller/action/id
這樣想着,我已經在我的RoutesConfig.cs
如下配置:
routes.MapRoute(
name: "AdminMessage",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Messages", id = UrlParameter.Optional }
);
下面是我的控制器看起來像
[HttpGet]
public ActionResult Messages(string id)
{
_model = new AdminViewModel();
_userName = Convert.ToString(HttpContext.User.Identity.Name);
ViewBag.Page = "Messages";
_model = PrepareLayoutModel(_userName, "Messages");
return View(_model);
}
而且我用Url.Action
以如下產生url
:
<a href="@Url.Action("Messages", "Admin", new RouteValueDictionary(new {message.MessageID}))"></a>
但它總是產生Url
爲http://domainname/controller/[email protected]#123#[email protected]
,而不是我想要生成它作爲http://domainname/controller/action/[email protected]#123#[email protected]
。那麼我能做到這一點的方式是什麼?
完美..愚蠢的錯誤繼續發生.. :( –
我可以再問一個疑問嗎?如何在'Razor視圖'中獲得'id'參數?我嘗試了'Request.QueryString [0]'和'Request.QueryString [「id」]'但它返回的時間都是null。現在最後一部分會被視爲'querystring'嗎?我怎樣才能得到這個? –
因爲你有'url:「{controller}/{action}/{id}」,那麼這個值會添加一個路由值,而不是查詢字符串值。但是因爲方法中的參數是'string id',那麼這個參數就是'message.MessageID'的值(你也可以使用'HttpContext.Current.Request.RequestContext.RouteData.Values [「id 「];'但這是不必要的) –