MVC中常见的路由设置

一.默认路由(MVC自带)

复制代码
public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
 
    routes.MapRoute( 
        "Default", // 路由名称 
        "{controller}/{action}/{id}", // 带有参数的 URL 
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 (UrlParameter.Optional-可选的意思)
    ); 
  } 
复制代码


二.不带参数的路由

 routes.MapRoute
(
"NoParameter", 
"{controller}/{action}/{id}"
);

三.带命名空间的路由

 routes.MapRoute(
              "AdminControllers", // 路由名称
              "{controller}/{id}-{action}", // 带有参数的 URL
              new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
              new string[] { "Admin.Controllers" }//命名空间
          );

四.带约束的路由规则(约束的意思就是用正则这类约束必须符合条件才可以)

routes.MapRoute(
                "RuleControllers",
                "{controller}/{action}-{Year}-{Month}-{Day}}",
                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
                new { Year = @"^\d{4}", Month = @"\d{2}" } //4位数 2位数
            );

五.带名称空间,带约束,带默认值的路由规则

复制代码
 routes.MapRoute(
                "Rule1",
                "Admin/{controller}/{action}-{Year}-{Month}-{Day}",
                new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
                new { Year = @"^\d{4}", Month = @"\d{2}" },
                new string[] { "Admin.Controllers" }
            );
复制代码

六.捕获所有的路由

  routes.MapRoute(
                "All", // 路由名称
                "{*Vauler}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在ASP.NET MVC使用Vue.js作为前端框架时,可以通过以下步骤配置路由: 1. 安装Vue.js和Vue Router ``` bash npm install vue vue-router --save ``` 2. 在Vue组件定义路由 在Vue组件定义路由,可以通过Vue Router来实现。例如: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import Home from './components/Home.vue' import About from './components/About.vue' Vue.use(VueRouter) const router = new VueRouter({ routes: [ { path: '/', component: Home }, { path: '/about', component: About } ] }) export default router ``` 以上代码,我们定义了两个路由,分别是'/'和'/about',对应的组件是Home和About。 3. 在MVC视图引入Vue组件 将Vue组件引入到MVC视图,可以通过以下方式实现: ```html <div id="app"> @Html.Partial("_VueComponent") </div> @section Scripts { <script src="@Url.Content("~/Scripts/vue/app.js")"></script> } ``` 其,_VueComponent是Vue组件的文件名,app.js是Vue实例化的入口文件。 4. 实例化Vue对象 在app.js,我们需要实例化Vue对象,并将路由配置传递给Vue对象。例如: ```javascript import Vue from 'vue' import App from './App.vue' import router from './router' new Vue({ el: '#app', router, render: h => h(App) }) ``` 以上代码,我们将路由配置传递给了Vue对象,并在render函数渲染了App.vue组件。 5. 在MVC控制器配置路由 最后,在MVC控制器配置路由,可以通过以下方式实现: ```csharp public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { return View(); } } ``` 以上代码,我们定义了两个Action,分别是Index和About,对应的视图是Index.cshtml和About.cshtml。在浏览器访问对应的URL时,会自动跳转到对应的视图页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值