简体   繁体   English

网址路由帮助Asp.net Mvc

[英]Url Routing Help Asp.net Mvc

I have three url types. 我有三种网址类型。 These: 这些:

first: http://localhost/ 首先: http:// localhost /
second: http://localhost/X/ 第二个: http:// localhost / X /
third: http://localhost/X/Y/ 第三名: http:// localhost / X / Y /

Examples Url: 示例网址:

http://localhost/test/ http:// localhost / test /
http://localhost/test/details/ http:// localhost / test / details /

first: 第一:

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

public class HomeController : Controller
{
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

}

second: 第二:

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

public class ModuleController : Controller
{
    //
    // GET: /Module/

    public ActionResult Index(string modul_name)
    {
        return View();
    }

}

third: 第三:

routes.MapRoute(
      "ModuleDetails",
      "{module_name}/{details_param}/{controller}/{action}", 
      new
      {
          controller = "ModuleDetails",
          action = "Index",
          module_name = UrlParameter.Optional,
          details_param = UrlParameter.Optional
      }
);

public class ModuleDetailsController : Controller
{
    //
    // GET: /ModuleDetails/

    public ActionResult Index(string modul_name, string details_param)
    {
        return View();
    }

}

in this instance; 在这种情况下;

http://localhost/X/ http:// localhost / X /
response: "Home", "Index" 回复: “首页”,“索引”

but; 但;

http://localhost/X/ http:// localhost / X /
response: Application in the server error. 响应:服务器中的应用程序错误。 Resource Not Found. 找不到资源。

http://localhost/X/Y/ http:// localhost / X / Y /
response: Application in the server error. 响应:服务器中的应用程序错误。 Resource Not Found. 找不到资源。

How can I do? 我能怎么做? Thanks, best regards.. 谢谢,最好的问候。

http://localhost/X/ response: Application in the server error. http:// localhost / X /响应:服务器中的应用程序错误。 Resource Not Found. 找不到资源。

This happens because each of your routes specifies at least 2 mandatory parameters. 发生这种情况是因为您的每个路由都至少指定了2个必需参数。

Try to add this one: 尝试添加此:

routes.MapRoute(
            "Default",
            "{controller}/{action}", 
            new
            {
                controller = "Home",
                action = "Index"
            }
        );

that's right friends.. 那是对的朋友..

        routes.MapRoute(
            "Default", // Route name
            "", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );

        routes.MapRoute(
            "Module",
            "{modul_name}",
            new { controller = "Modul", action = "Index", modul_name = UrlParameter.Optional }
        );

        routes.MapRoute(
            "Page",
            "{modul_name}/{page_name}",
            new { controller = "Page", action = "Index", modul_name = UrlParameter.Optional, page_name = UrlParameter.Optional }
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM