ASP.NET MVC出力生成Urlリンク詳細
ASP.NET mvcでよくページジャンプや出力Urlリンクがあるときは、ASPについてお話しします.NET MVCでどのように出力して友好的なUrlリンクを生成します.よく使われる方法は3つあると知っています.
私のGlobalasaxは、以下のようにルーティングを登録します.
私のGlobalasaxは、以下のようにルーティングを登録します.
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional });
- }
一、ASP.NET MVC在视图中生成Url链接
1、采用Html Helper方法
最简单的方式就是采用Html.ActionLink方法:
输出的Html为:
- @Html.ActionLink("About this application", "About")
href="/Home/About">About this application
Controller, , Html.ActionLink Controller Controller。Html.ActionLink 9 , 。 Html.ActionLink 。
: class @, class , @ 。
Html @Html.ActionLink(" ", "About") たちについて @Html.ActionLink(" ","About","Home") たちについて @Html.ActionLink(" ", "About", "Home", new {@class="curr",id="link1",name="link1",target="_blank" }) たちについて @Html.ActionLink(" ", "About",new {controller="Home"}) たちについて 2、
MVC Url Html.ActionLink , 。
Html.ActionLink A , Html.ActionLink ? Html.ActionLink ,Html.ActionLink 。 。
:
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("NewRoute", "App/Do{action}",
- new { controller = "Home" });
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional });
- }
, Url :
- @Html.ActionLink(" ","About","Home")
, Html.ActionLink Url , controller Home /App/Do 。
: Controller 。 :App/Do{action}, action。 Html.ActionLink Url 。
Html @Html.ActionLink(" ", "About") たちについて @Html.ActionLink(" ","About","Home") たちについて @Html.ActionLink(" ", "About", "Home", new {@class="curr",id="link1",name="link1",target="_blank" }) たちについて @Html.ActionLink(" ", "About",new {controller="Home"}) たちについて
3、ASP.NET MVC Controller
Html.ActionLink Controller , Controller。 Controller , Html.ActionLink Controller 。
- @Html.ActionLink("About this application", "About", "MyController")
Url :
href="/MyController/About">About this application
: Html.ActionLink Url ,MVC Controller Action , Controller Action, , 。
4、 ASP.NET MVC
ASP.NET MVC URL ( controller、action ) RouteData 。 Html.ActionLink routeValues , object。 , :
- @Html.ActionLink(" ", "About",new {controller="Home"})
controller , id :
:
- @Html.ActionLink("About this application", "About", new { id = "MyID" })
href="/Home/About/MyID">About this application
:
- routes.MapRoute("MyRoute", "{controller}/{action}/{color}/{page}");
ActionLink :
- @Html.ActionLink("Click me", "List", "Catalog", new {page=789}, null)
href="/Catalog/List/Purple/789">Click me
ActionLink :
url , :
- @Html.ActionLink("Click me", "List", "Catalog", new {page=789,pwd="2"}, null)
href="/Catalog/List/Purple/789?pwd=2">Click me
5、 Html.ActionLink html
A class、id、target html ,Html.ActionLink 。
:
- @Html.ActionLink("About this application", "Index", "Home", null,
- new {id = "myAnchorID", @class = "myCSSClass",target="_blank"})
class="myCSSClass" href="/" id="myAnchorID" target="_blank">About this application
6、 Url
Url, http, 。 :
:
- @Html.ActionLink("About this application", "Index", "Home",
- "https", "myserver.mydomain.com", " myFragmentName",
- new { id = "MyId"},
- new { id = "myAnchorID", @class = "myCSSClass"})
class="myCSSClass" href="https://myserver.mydomain.com/Home/Index/MyId#myFragmentName"
id="myAnchorID">About this application
8、 Url
URl , A , Url.Action Url 。
:
- My URL is: @Url.Action("Index", "Home", new { id = "MyId" })
My URL is: /Home/Index/MyId
9、 Routing Data Url
ASP.NET MVC Html.ActionLink Url.Action Url, Html.RouteLink Url.RouteUrl Url。Html.RouteLink Url.RouteUrl Routing Data, 。
, :
- @Html.RouteLink("Routed Link", new { controller = "Home", action = "About", id="MyID"})
- @Url.RouteUrl(new { controller = "Home", action = "About", id = "MyID" })
href="/Home/About/MyID">Routed Link
/Home/About/MyID
、ASP.NET MVCがActionメソッドでUrlリンクを
ビューでUrlリンクを するほか、ActionでActionジャンプを するなど、Actionでも されます.
public ViewResult MyActionMethod() {
string myActionUrl = Url.Action("Index", new { id = "MyID"});
string myRouteUrl = Url.RouteUrl(new { controller = "Home", action = "Index"});
//... do something with URLs...
}
ASP.NET MVCのActionメソッドでのリダイレクトとジャンプ:
public ActionResult MyActionMethod() {
return RedirectToAction("Index");
}
にはControllerが されていません.デフォルトは のControllerです. のControllerのアクションにジャンプするには、RedirectionToActionのリロード またはRedirectToRouteを び します. のようになります.
public ActionResult MyActionMethod() {
return RedirectToAction("Index","Home");
}
public ActionResult MyOtherActionMethod() {
return RedirectToRoute(new { controller = "Home", action = "Index", id = "MyID"});
}
、ASP.NET MVCは、Urlリンクを するために のルーティングルールを する
ルーティングルールを する には、 のように (MapRouteの のパラメータ)を します.
routes.MapRoute("MyRoute", "{controller}/{action}");
routes.MapRoute("MyOtherRoute", "App/{action}", new { controller = "Home"});
ASP.NET MVCではルーティングの (MyRouteとMyOtherRoute)を する が2つあります.
、タグルーティングの 、
、Urlリンクを するために の のルートを することができる
MyRouteとMyOtherRouteの2つのルートを しました.この には の があり、 に れたものが に されます. えば、 のコードを び すとします.
@Html.ActionLink("Click me", "About");
されたUrlリンクは にMyRouteというルートを しており、 のルートを する はHtmlを します.RouteLinkは、 のルーティングルール を して します.
@Html.RouteLink("Click me", "MyOtherRoute", new { action = "About"});