微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将参数传递给ASP.NET MVC中的控制器

如何解决将参数传递给ASP.NET MVC中的控制器

我正在遵循指南here。当我请求URL http:// localhost:xxxxx / Employee /时,我想返回。但是,使用下面的代码,这将始终返回404代码。在RouteConfig.cs中,如果将"Employee/{name}"更改为"{controller}/{name}",则会返回空白页面

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

Global.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);
        }
    }
}

RouteConfig.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
               });
        }
    }
}

EmployeeController.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);
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。