首页 > 解决方案 > 将参数传递给 ASP.NET MVC 中的控制器

问题描述

我在这里按照指南进行操作。当我请求 URL http://localhost:xxxxx/Employee/ 时,我想返回 . 但是,使用下面的代码,这将始终返回 404 代码。在 RouteConfig.cs 中,如果我更改"Employee/{name}""{controller}/{name}"这将返回一个空白页。

我在这里做错了什么?我看了几次教程,但不知道有什么不同。

全球.asax

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

namespace scratch {
    public class MvcApplication : System.Web.HttpApplication {
        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

路由配置.cs

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

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

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

            routes.MapRoute(
               "Employee",
               "Employee/{name}", 
               new {
                   controller = "Employee",
                   action = "Search",
                   name = UrlParameter.Optional
               });
        }
    }
}

员工控制器.cs

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace scratch.Controllers {
    public class EmployeeController : Controller {
        public ActionResult Search(string name) {
            var input = Server.HtmlEncode(name);
            return Content(input);
        }
    }
}

标签: c#asp.net-mvchttp

解决方案


推荐阅读