ASP.NET MVC 3路由路由、ASP、NET、MVC

2023-09-03 15:26:12 作者:薄荷糖、微凉

我想创建航线。

这是

/emlak/TITLE/number.aspx

/emlak/Here_is_your_best_property/123456.aspx

Global.asax中:

Global.asax:

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index" },
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

我控制器

namespace emrex.Controllers
{
    public class EmlakController : Controller
    {
        //
        // GET: /Emlak/

        public ActionResult Index(String productId, String deli)
        {
            return View();
        }

    }
}

和我得到一个错误:

在/应用程序中的服务器错误。

Server Error in '/' Application.

资源无法找到。

感谢您的帮助。

推荐答案

您的问题是(至少当我想你的code)你有指定的路由约束,他们真的不应该。我能得到这个通过做工作得很好:

Your problem is (at least when I tried your code) you have route constraints specified where they really shouldn't be. I was able to get this to work just fine by doing:


     routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index", productId = UrlParameter.Optional, deli = UrlParameter.Optional }
);

尝试 - ?有什么区别

Try that - any difference?