c# - 将参数传递给 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);
}
}
}
解决方案
推荐阅读
- r - 垂直重组数据
- postgresql - PostgreSQL 如何知道我当前是否在 TimeZone 'CET' 中使用 DST?
- r - 将数据框转换为 1 和 0
- c - 为什么这会创建两个输入而不是一个
- amazon-web-services - AWS SQS 消息转发到下一个 sqs 服务不显示 cloudwatch 日志
- php - Laravel - 使用 Laravel Eloquent 从两个表中获取数据
- javascript - Angular 材料选择在应用程序启动时不显示当前的 ngx-translate 语言(在用户手动选择语言之前)
- c++ - 使用 MPI_Scatter 时出现 Seg 错误
- python - 证明山构造算法的正确性
- google-cloud-platform - 您如何以编程方式在 Google 部署管理器中导入文件