zoukankan      html  css  js  c++  java
  • ASP.NET MVC2 第八章Ⅱ

    §8.3  Generating Outgoing URLs

    从前, 如果你要从detail.aspx页面中查找一个id参数, 你会这么写

    myHyperLink.NavigateUrl = "~/Details.aspx?id=" + itemID;
    但是, 现在我们只需要这样写:
    <a href="/Products/Details/<%: ViewData["ItemID"] %>">More details</a>

    §8.3.1  Generating Hyperlinks with Html.ActionLink()

    最简单的产生url的方式就是从一个视图模板调用Html.ActionLink().比如:

    <%: Html.ActionLink("See all of our products", "List", "Products") %>

    它的实现效果如下

    <a href="/Products/List">See all of our products</a>

    注意, 这里如果你没有指定controller, 他会默认你当前的controller

    Passing Extra Parameters

    你可以传递额外的route入口需要的参数.

    <%: Html.ActionLink("Red items", "List", "Products",new { color="Red", page=2 }, null) %>

    它的实现效果:

    <a href="/Products/List?color=Red&amp;page=2">Red items</a>

    如果,你的route配置中有这样的配置方式Products/List/{color}/{page},那么就会产生如下的代码

    <a href="/Products/List/Red/2">Red items</a>

    How Parameter Defaults Are Handled

    当我们写下下面的代码的时候, 而且product的默认action是index

    <%: Html.ActionLink("Products homepage", "Index", "Products") %>

    它的实现效果如下

    <a href="/Products">Products homepage</a>

    这里因为默认情况下相同, 所以省略了index

    Generating Fully Qualified Absolute URLs

    Html.ActionLink() 通常生成的是URL的一部分,(i.e. /Products,而不是http://www.example.com/Products). 如果你想生成一个完整的URL, 如下:

    <%: Html.ActionLink("Click me", "MyAction", "MyController", "https",
    	"www.example.com", "anchorName", new { param = "value" },
    	new { myattribute = "something" }) %>

    它的效果如下:

    <a myattribute="something" href="https://www.example.com/MyController/MyAction?param=value#anchorName">Click me</a>
     

    §8.3.2 Generating Links and URLs from Pure Routing Data

    Html.RouteLink()Html.ActionLink() 是类似的.

    <%: Html.RouteLink("Click me", new { controller = "Products", action = "List" }) %>

    它会产生如下的链接

    <a href="/Products/List">Click me</a>
    同样的,Url.RouteUrl()Url.Action() 是类似的 .
    <%: Url.RouteUrl(new { controller = "Products", action = "List" }) %>

    产生如下的URL:  /Products/List

    在mvc应用程序中, 这些是不常用的, 但是熟悉他们还是有好处的.

    §8.3.3  Performing Redirections to Generated URLs

    实现一个重定向功能, 只要简单的返回RedirectToAction()结果, 并且传入目标controller和action方法.

    public ActionResult MyActionMethod()
    {
    	return RedirectToAction("List", "Products");
    }

    如果你想获取一个URL字符串, 而不是实现HTTP重定向, 你可以做如下:

    public ActionResult MyActionMethod()
    {
    	string url = Url.Action("SomeAction", new { customerId = 456 });
    	// ... now do something with url
    }

    §8.3.4  Understanding the Outbound URL-Matching Algorithm

    §8.3.5  Generating Hyperlinks with Html.ActionLink<T> and Lambda Expressions

    §8.3.6  Working with Named Routes

     
  • 相关阅读:
    12.python笔记之mysqldb模块
    13.python笔记之pyyaml模块
    11.python之线程,协程,进程,
    2.saltstack笔记之目标,模块,返回写入数据库
    6.django笔记之orm
    5.django笔记之form保存表单信息,动态select
    4.django笔记之admin
    docker批量删除none镜像
    docker 给none镜像打镜像
    jenkins卡在等待界面解决方法
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1863850.html
Copyright © 2011-2022 走看看