ASP.NET MVC出力生成Urlリンク詳細

41978 ワード

ASP.NET mvcでよくページジャンプや出力Urlリンクがあるときは、ASPについてお話しします.NET MVCでどのように出力して友好的なUrlリンクを生成します.よく使われる方法は3つあると知っています.
私のGlobalasaxは、以下のようにルーティングを登録します.
 
  
  1. public static void RegisterRoutes(RouteCollection routes) {
  2. routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
  3. new { controller = "Home", action = "Index", id = UrlParameter.Optional });
  4. }

一、ASP.NET MVC在视图中生成Url链接

1、采用Html Helper方法

最简单的方式就是采用Html.ActionLink方法:

 
  
  1. @Html.ActionLink("About this application", "About")
输出的Html为:
  1. href="/Home/About">About this application
  2. Controller, , Html.ActionLink Controller Controller。Html.ActionLink 9 , 。 Html.ActionLink 。



    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"})
    たちについて
    class @, class , @ 。

    2、

    MVC Url Html.ActionLink , 。

    たちについて

    Html.ActionLink A , Html.ActionLink ? Html.ActionLink ,Html.ActionLink 。 。

    1. public static void RegisterRoutes(RouteCollection routes) {
    2. routes.MapRoute("NewRoute", "App/Do{action}",
    3. new { controller = "Home" });
    4. routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
    5. new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    6. }
     
      
    1. @Html.ActionLink(" ","About","Home")
    , Url :

    たちについて

    , Html.ActionLink Url , controller Home /App/Do 。


    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"})
    たちについて
    Controller 。 :App/Do{action}, action。 Html.ActionLink Url 。

    3、ASP.NET MVC Controller

    Html.ActionLink Controller , Controller。 Controller , Html.ActionLink Controller 。

     
      
    1. @Html.ActionLink("About this application", "About", "MyController")


    Url :

    1. href="/MyController/About">About this application
    2. Html.ActionLink Url ,MVC Controller Action , Controller Action, , 。

      4、 ASP.NET MVC

      ASP.NET MVC URL ( controller、action ) RouteData 。 Html.ActionLink routeValues , object。 , :

      1. @Html.ActionLink(" ", "About",new {controller="Home"})


      controller , id :

       
        
      1. @Html.ActionLink("About this application", "About", new { id = "MyID" })
      1. href="/Home/About/MyID">About this application
        1. routes.MapRoute("MyRoute", "{controller}/{action}/{color}/{page}");
         
          
        1. @Html.ActionLink("Click me", "List", "Catalog", new {page=789}, null)
        ActionLink :
        1. href="/Catalog/List/Purple/789">Click me
        2. ActionLink :
          1. @Html.ActionLink("Click me", "List", "Catalog", new {page=789,pwd="2"}, null)
          url , :
          1. href="/Catalog/List/Purple/789?pwd=2">Click me
          2. 5、 Html.ActionLink html A class、id、target html ,Html.ActionLink 。
             
              
            1. @Html.ActionLink("About this application", "Index", "Home", null,
            2. new {id = "myAnchorID", @class = "myCSSClass",target="_blank"})
            1. class="myCSSClass" href="/" id="myAnchorID" target="_blank">About this application
            2. 6、 Url Url, http, 。 :
               
                
              1. @Html.ActionLink("About this application", "Index", "Home",
              2. "https", "myserver.mydomain.com", " myFragmentName",
              3. new { id = "MyId"},
              4. new { id = "myAnchorID", @class = "myCSSClass"})
              :
              1. class="myCSSClass" href="https://myserver.mydomain.com/Home/Index/MyId#myFragmentName"
              2. id="myAnchorID">About this application 8、 Url URl , A , Url.Action Url 。
                 
                  
                1. My URL is: @Url.Action("Index", "Home", new { id = "MyId" })
                1. My URL is: /Home/Index/MyId
                2. 9、 Routing Data Url ASP.NET MVC Html.ActionLink Url.Action Url, Html.RouteLink Url.RouteUrl Url。Html.RouteLink Url.RouteUrl Routing Data, 。
                   
                    
                  1. @Html.RouteLink("Routed Link", new { controller = "Home", action = "About", id="MyID"})
                  2. @Url.RouteUrl(new { controller = "Home", action = "About", id = "MyID" })
                  , :
                  1. href="/Home/About/MyID">Routed Link

                  2. /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"});