码迷,mamicode.com
首页 > Web开发 > 详细

mvc路由特性学习1

时间:2015-07-20 01:12:29      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

修改App_Start/RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Web
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            //注释掉系统生成的代码后,自定义路由。
            routes.MapMvcAttributeRoutes();

            //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    name: "Default",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            //);
        }
    }
}

 

AccountController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    //[Route("account/{action}")]//给控制器(类)中的操作方法定义一个全局的规则,如果想对单独的操作方法重写访问路由,那么在对应的操作方法上定义特性即可,在单独的操作的方法上定义特性会覆盖全局的路由规则。
    [RoutePrefix("account")]//给当前控制器中的操作方法给定一个默认的前缀,当操作方法没有加控制器名称的时候会自动的为每个控制器增加前缀,从而避免写重复的代码
    [Route("{action}")]
    public class AccountController : Controller
    {
        // GET: Account
        //[Route("account/{year}/{month}/{day}")]//从用户请求的url通过路由规则映射到此操作方法
        [Route("~/")]//使用~/作为当前路由模板的开头
        [Route("")]//增加/路由的支持
        [Route("index")]
        public ActionResult Index(string year="",string month="",string day="")
        {
            ViewBag.Message = year + month + day+"---";
            return View();
        }
    }
}

 

mvc路由特性学习1

标签:

原文地址:http://www.cnblogs.com/frank888/p/4660263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!