当前位置:编程学习 > C#/ASP.NET >>

MVC4设置RouteConfig优化url的问题 求教。

配置了半天 没搞出来 
比如我现在有个url是

http://xxx.com/show?s=1234

controller为show
action 为index

我现在想要的效果是url效果为

http://xxx.com/show/1234

我现在写的是
 routes.MapRoute(
             name: "Show",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Show", action = "Index", id = UrlParameter.Optional }
         );

但是貌似配置错误找不到主页

这里应该如何设置啊? --------------------编程问答-------------------- showController.cs 的内容贴出来看看 --------------------编程问答--------------------
routes.MapRoute(
             "Show",
             "{controller}/{id}",
             new { controller = "Show" , id = UrlParameter.Optional }
             new { controller = "Show", action = "Index" }     
         );


引用 楼主 lijing3333 的回复:
配置了半天 没搞出来 
比如我现在有个url是

http://xxx.com/show?s=1234

controller为show
action 为index

我现在想要的效果是url效果为

http://xxx.com/show/1234

我现在写的是
 routes.MapRoute(
             name: "Show",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Show", action = "Index", id = UrlParameter.Optional }
         );

但是貌似配置错误找不到主页

这里应该如何设置啊?
--------------------编程问答--------------------
引用 2 楼 zhuankeshumo 的回复:
routes.MapRoute(
             "Show",
             "{controller}/{id}",
             new { controller = "Show" , id = UrlParameter.Optional }
             new { controller = "Show", action = "Index" }     
         );


Quote: 引用 楼主 lijing3333 的回复:

配置了半天 没搞出来 
比如我现在有个url是

http://xxx.com/show?s=1234

controller为show
action 为index

我现在想要的效果是url效果为

http://xxx.com/show/1234

我现在写的是
 routes.MapRoute(
             name: "Show",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Show", action = "Index", id = UrlParameter.Optional }
         );

但是貌似配置错误找不到主页

这里应该如何设置啊?


不行啊 http://localhost:25606/show/15974 还是报错。。。 --------------------编程问答--------------------
引用 1 楼 banian_cn 的回复:
showController.cs 的内容贴出来看看



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

namespace NyWeb.Controllers
{
    public class ShowController : Controller
    {
        //
        // GET: /Show/

        public ActionResult Index(int s)
        {
            if (!string.IsNullOrEmpty(s.ToString()))
            {
                Ny.Model.NyModels model = new Ny.BLL.NyShare().GetShareShowModel(s);

                if (model != null)
                    return View(model);
            }

            return View();
        }

        [ChildActionOnly]
        public PartialViewResult _CommentList(string ShareId)
        {
            List<Ny.Model.NyModels> modelList = new Ny.BLL.NyComment().ExecCommentByShareId("CommentByShareId", new SqlParameter("@startIndex", 1),
                    new SqlParameter("@endIndex", 8), new SqlParameter("@shareId", ShareId));
            if (modelList.Count > 0)
                return PartialView(modelList);
            else
                return PartialView();
        }

        [ChildActionOnly]
        public PartialViewResult _ShowListRecommend(int p)
        {
            List<Ny.Model.NyMenuGrandson> modelList = new Ny.BLL.NyMenuGrandson().GetListByDisplay(p);

            if (modelList != null)
                return PartialView(modelList);
            else
                return PartialView();
        }

        [ChildActionOnly]
        public PartialViewResult _ShowListRecommendItems(int g, int shareid)
        {
            List<Ny.Model.NyModels> modelList = new Ny.BLL.NyShare().GetShareByMenuGrandsonRecommend(g, shareid);

            if (modelList != null)
                return PartialView(modelList);
            else
                return PartialView();
        }

        [ChildActionOnly]
        public string _ShareListRecommendItems(int p,int shareid)
        {
            List<Ny.Model.NyModels> modelList = new Ny.BLL.NyShare().GetShareByMenuParentRecommend(p,shareid);

            if (modelList.Count > 0)
            {
                return NyWeb.Tools.ShareList.GetShareListHtml(modelList);
            }
            else
                return "NO";
        }

    }
}

--------------------编程问答-------------------- http://xxx.com/show/index/1234 --------------------编程问答--------------------
引用 5 楼 fangxuan 的回复:
http://xxx.com/show/index/1234


对于“NyWeb.Controllers.ShowController”中方法“System.Web.Mvc.ActionResult Index(Int32)”的不可以为 null 的类型“System.Int32”的参数“s”,参数字典包含一个 null 项。可选参数必须为引用类型、可以为 null 的类型或声明为可选参数。
参数名: parameters

报错 我如何不要那个index 直接就是  xxx.com/show/12345 --------------------编程问答--------------------
引用 3 楼 lijing3333 的回复:
Quote: 引用 2 楼 zhuankeshumo 的回复:

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


Quote: 引用 楼主 lijing3333 的回复:

配置了半天 没搞出来 
比如我现在有个url是

http://xxx.com/show?s=1234

controller为show
action 为index

我现在想要的效果是url效果为

http://xxx.com/show/1234

我现在写的是
 routes.MapRoute(
             name: "Show",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Show", action = "Index", id = UrlParameter.Optional }
         );

但是貌似配置错误找不到主页

这里应该如何设置啊?


不行啊 http://localhost:25606/show/15974 还是报错。。。
报什么错贴出来  --------------------编程问答--------------------
引用 7 楼 zhuankeshumo 的回复:
Quote: 引用 3 楼 lijing3333 的回复:

Quote: 引用 2 楼 zhuankeshumo 的回复:

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


Quote: 引用 楼主 lijing3333 的回复:

配置了半天 没搞出来 
比如我现在有个url是

http://xxx.com/show?s=1234

controller为show
action 为index

我现在想要的效果是url效果为

http://xxx.com/show/1234

我现在写的是
 routes.MapRoute(
             name: "Show",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Show", action = "Index", id = UrlParameter.Optional }
         );

但是貌似配置错误找不到主页

这里应该如何设置啊?


不行啊 http://localhost:25606/show/15974 还是报错。。。
报什么错贴出来 




HTTP 错误 404.0 - Not Found
您要找的资源已被删除、已更名或暂时不可用。

http://localhost:25606/show/15970
http://localhost:25606/show?s=15970 
都报404 --------------------编程问答-------------------- 放在default路由前面 id是必须的
         routes.MapRoute(
              name: "Show",
              url: "{controller}/{id}",
              defaults: new { controller = "Show", Index= "Index" },
              constraints: new { controller = "Show", action = "Index", id=@"\d+" }
            ); --------------------编程问答--------------------
引用 9 楼 zhuankeshumo 的回复:
放在default路由前面 id是必须的
         routes.MapRoute(
              name: "Show",
              url: "{controller}/{id}",
              defaults: new { controller = "Show", Index= "Index" },
              constraints: new { controller = "Show", action = "Index", id=@"\d+" }
            );


还是不行 啊 我是放在default的前面  我的路由设置贴出来 请看看


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

namespace NyWeb
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

            routes.MapRoute(
               name: "Show",
               url: "{controller}/{id}",
               defaults: new { controller = "Show", Index = "Index" },
               constraints: new { controller = "Show", action = "Index", id = @"\d+" }
             );

           

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

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }
    }
}
--------------------编程问答--------------------
引用 10 楼 lijing3333 的回复:
Quote: 引用 9 楼 zhuankeshumo 的回复:

放在default路由前面 id是必须的
         routes.MapRoute(
              name: "Show",
              url: "{controller}/{id}",
              defaults: new { controller = "Show", Index= "Index" },
              constraints: new { controller = "Show", action = "Index", id=@"\d+" }
            );


还是不行 啊 我是放在default的前面  我的路由设置贴出来 请看看


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

namespace NyWeb
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

            routes.MapRoute(
               name: "Show",
               url: "{controller}/{id}",
               defaults: new { controller = "Show", Index = "Index" },
               constraints: new { controller = "Show", action = "Index", id = @"\d+" }
             );

           

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

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }
    }
}
--------------------编程问答--------------------
       routes.MapRoute(
          name: "Show",
          url: "{controller}/{id}",
          defaults: new { controller = "Show", action = "Index" },
          constraints: new { controller = "Show", action = "Index", id = @"\d+" }
        ); --------------------编程问答-------------------- 上面action 写错了

  routes.MapRoute(
               name: "Show",
               url: "{controller}/{id}",
               defaults: new { controller = "Show", Index = "Index" },
               constraints: new { controller = "Show", action = "Index", id = @"\d+" }
             ); --------------------编程问答--------------------
引用 13 楼 zhuankeshumo 的回复:
上面action 写错了

  routes.MapRoute(
               name: "Show",
               url: "{controller}/{id}",
               defaults: new { controller = "Show", Index = "Index" },
               constraints: new { controller = "Show", action = "Index", id = @"\d+" }
             );


你controller 是怎么写的啊? 我怎么不行呢? --------------------编程问答--------------------
        public ActionResult Index(int id)
        { 
            return View();
        } --------------------编程问答-------------------- 你要么给AccountLogin这个路由加个constraints,要么把Show这个路由放在最前面,否则http://localhost:25606/show/15970这个的url会先匹配路由AccountLogin,你可以用route debugger测试一下看:http://nuget.org/packages/routedebugger/
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,