0

当我将浏览器导航到 url 时出现此错误:

localhost:10793/RealEstates/10

这是我的 RouteConfig 代码:

    public class RouteConfig
    {
       public static void RegisterRoutes(RouteCollection routes)
       {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}",
            defaults: new { controller = "Main", action = "Index" }
        );
        routes.MapRoute(
            name: "RealEstates",
            url: "RealEstates/{action}",
            defaults: new { controller = "RealEstates", action = "Index" }
        );
        routes.MapRoute(
            name: "RealEstatesViewAd",
            url: "RealEstates/{id}",
            defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
        );
    }

}

我的错误:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

当将代码更改为:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}",
        //    defaults: new { controller = "Main", action = "Index" }
        //);
        //routes.MapRoute(
        //    name: "RealEstates",
        //    url: "RealEstates/{action}",
        //    defaults: new { controller = "RealEstates", action = "Index" }
        //);
        routes.MapRoute(
            name: "RealEstatesViewAd",
            url: "RealEstates/{id}",
            defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
        );
    }

}

这是工作,但是当我在控制器中调用其他操作时

本地主机:10793/RealEstates/CreateAd

发现这个错误

参数字典包含“Youe3lan.Controllers.RealEstatesController”中方法“System.Web.Mvc.ActionResult ViewAd(Int32)”的不可为空类型“System.Int32”的参数“id”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数

这是我的控制器:

    namespace MvcAppliction1.Controllers
{    
    public class RealEstatesController : Controller
    {        
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ViewAd(int id)
        {
            return View();
        }
        public ActionResult CreateAd()
        {
            return View();
        }
    }
}
4

2 回答 2

1

您需要将其更改为:

routes.MapRoute(
        name: "RealEstatesViewAd",
        url: "RealEstates/{action}/{id}",
        defaults: new { controller = "RealEstates", action = "ViewAd", id UrlParameter.Optional }
    );}}

看看这里它可能会有所帮助:

http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx

更新

将此添加到您的控制器:

public ActionResult ViewAd(Int32 id)
{
    return View();
}

你看

本地主机:10793/RealEstates/10

被翻译成:

本地主机:10793/RealEstates/ViewAdd/10

因此,您需要控制器中的该方法接受 it 参数。

于 2013-04-12T15:52:46.573 回答
0

您已将路线中的 id 标记为可选:

id = UrlParameter.Optional

但我敢打赌你的控制器不可为空??

public ActionResult ViewAd(Int32 id)
{

}

因此,即使路线允许,您也不能将 null 发布到您的 id 中。如果您将其更改为:

public ActionResult ViewAd(Int32? id)
{

}

您不会收到错误消息:

参数字典包含“Youe3lan.Controllers.RealEstatesController”中方法“System.Web.Mvc.ActionResult ViewAd(Int32)”的不可为空类型“System.Int32”的参数“id”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数

于 2013-04-12T15:59:01.540 回答