MVC---输出URL

<strong>一. 在视图中生成输入URL</strong>
如一个静态的a元素:

<a href="/Home/CustomVariable">This is an outgoing URL</a>

手工硬编码URL的方法乏味和易错。

<strong>1.1 用路由系统生成输入URL</strong>
初始时:RouteConfig中的路由为:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapMvcAttributeRoutes();

            routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                });
        }

在View中生成输出URL最简单的方法:在View中使用Html.ActionLink方法。

    <div>
        @Html.ActionLink("This is an outgoing URL", "CustomVariable")
    </div>

效果虽然和手动写链接一样,但是,加入现在新加了路由定义:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapMvcAttributeRoutes();

            routes.MapRoute("NewRoute", "App/Do{action}",
                new { controller = "Home" });

            routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                });
        }

此时ActionLink所生成的HTMl为:

<a href="/App/DoCustomVariable">This is an outgoing URL</a>

即:改变定义URL方案的路由,会改变输出URL的生成方式。
<strong>1.2 以其它Controller为目标生成输入URL</strong>

    <div>
        @Html.ActionLink("This is an outgoing URL", "Index", "Admin");
    </div>

生成的URL为

http://localhost:24903/Admin

<strong>1.3 传递额外的值</strong>
可以通过匿名类给一些片段传递值,匿名类中以属性表示片段。

@Html.ActionLink("This is an outgoing URL", "CustomVariable", new { id = "Hello" })

得到:

http://localhost:24903/Admin/CustomVariable/Hello

<strong>1.4 指定HTML标签属性</strong>
通过一个匿名类,可以给a元素设置属性:

    <div>
        @Html.ActionLink("This is an outgoing URL", "Index", "Home", null, new { id = "myAnchorID", @class = "myCssClass" })
    </div>

这个匿名类具有id和class属性,@class表示使用关键字作为class成员的名字。
得到的HTML为:

<a class="myCssClass" href="/App/DoIndex" id="myAnchorID">This is an outgoing URL</a>

<strong>1.5 生成链接中的全限定URL</strong>

        @Html.ActionLink("This is an outgoing URl", "Index", "Home",
       "https", "myserver.mydomain.com", " myFragmentname", new { id = "MyId" },
       new { id = "myAnchorID", @class = "myCssClass" })

生成的HTML为:

<a class="myCssClass" href="https://myserver.mydomain.com/App/DoIndex?id=MyId# myFragmentname" id="myAnchorID">This is an outgoing URl</a>

<strong>1.6 生成URL,而不是链接</strong>
可以使用Url.Action方法只生成URL而不生成HTML:

    <div>
        this is a URL:
        @Url.Action("Index", "Home", new { id = "myId" })
    </div>

生成了URL:

this is a URL: /Home/Index/myId

<strong>1.7 在Action中生成输入URL</strong>

        public ViewResult MyActionMethod()
        {
            string myActionUrl = Url.Action("Index", new { id = "MyID" });
            string myRouteUrl = Url.RouteUrl(new { controller = "Home", action = "Index" });
            return View();
        }

更普遍的需求是,把浏览器重定向到另一个URL:

        public RedirectToRouteResult MyActionMethod()
        {
            return RedirectToAction("Index");
        }

或者

        public RedirectToRouteResult MyActionMethod()
        {
            return RedirectToRoute(new { controller = "Home", action = "Index", id = "MyID" });
        }

<strong>二. 使用区域</strong>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,314评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,046评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,898评论 18 399
  • 我总记得小时候自己趴在自家楼房一楼的大铁门前,眼巴巴的看着邻居小伙伴们穿梭来去的影子,没有人和我玩,偶尔会...
    让我随心而行阅读 1,501评论 0 1
  • 大鹏鸟为了飞向南方,努力地练习自己的飞翔的本领,等待承载它的十二级的大风来承载它。而知了和小愿意飞就飞,它...
    心向光阳阅读 2,695评论 0 1