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

lijing3333 2013-07-13 12:08:03
配置了半天 没搞出来
比如我现在有个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 }
);

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

这里应该如何设置啊?
...全文
1175 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtee 2013-07-13
  • 打赏
  • 举报
回复
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 } ); 但是貌似配置错误找不到主页 这里应该如何设置啊?
webdiyer 2013-07-13
  • 打赏
  • 举报
回复
你要么给AccountLogin这个路由加个constraints,要么把Show这个路由放在最前面,否则http://localhost:25606/show/15970这个的url会先匹配路由AccountLogin,你可以用route debugger测试一下看:http://nuget.org/packages/routedebugger/
newtee 2013-07-13
  • 打赏
  • 举报
回复
public ActionResult Index(int id) { return View(); }
lijing3333 2013-07-13
  • 打赏
  • 举报
回复
引用 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 是怎么写的啊? 我怎么不行呢?
newtee 2013-07-13
  • 打赏
  • 举报
回复
上面action 写错了 routes.MapRoute( name: "Show", url: "{controller}/{id}", defaults: new { controller = "Show", Index = "Index" }, constraints: new { controller = "Show", action = "Index", id = @"\d+" } );
newtee 2013-07-13
  • 打赏
  • 举报
回复
routes.MapRoute( name: "Show", url: "{controller}/{id}", defaults: new { controller = "Show", action = "Index" }, constraints: new { controller = "Show", action = "Index", id = @"\d+" } );
newtee 2013-07-13
  • 打赏
  • 举报
回复
引用 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
);
}
}
}
[/quote]
lijing3333 2013-07-13
  • 打赏
  • 举报
回复
引用 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
            );
        }
    }
}
newtee 2013-07-13
  • 打赏
  • 举报
回复
放在default路由前面 id是必须的 routes.MapRoute( name: "Show", url: "{controller}/{id}", defaults: new { controller = "Show", Index= "Index" }, constraints: new { controller = "Show", action = "Index", id=@"\d+" } );
Banianer 2013-07-13
  • 打赏
  • 举报
回复
showController.cs 的内容贴出来看看
lijing3333 2013-07-13
  • 打赏
  • 举报
回复
引用 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 } ); 但是貌似配置错误找不到主页 这里应该如何设置啊?
[/quote] 不行啊 http://localhost:25606/show/15974 还是报错。。。[/quote]报什么错贴出来 [/quote] HTTP 错误 404.0 - Not Found 您要找的资源已被删除、已更名或暂时不可用。 http://localhost:25606/show/15970 http://localhost:25606/show?s=15970 都报404
newtee 2013-07-13
  • 打赏
  • 举报
回复
引用 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 } ); 但是貌似配置错误找不到主页 这里应该如何设置啊?
[/quote] 不行啊 http://localhost:25606/show/15974 还是报错。。。[/quote]报什么错贴出来
lijing3333 2013-07-13
  • 打赏
  • 举报
回复
引用 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
白云任去留 2013-07-13
  • 打赏
  • 举报
回复
http://xxx.com/show/index/1234
lijing3333 2013-07-13
  • 打赏
  • 举报
回复
引用 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";
        }

    }
}

lijing3333 2013-07-13
  • 打赏
  • 举报
回复
引用 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 } ); 但是貌似配置错误找不到主页 这里应该如何设置啊?
[/quote] 不行啊 http://localhost:25606/show/15974 还是报错。。。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧