[英]Catch the parameters from url in asp.net MVC
嗨,我是初学者,可以从指导点网站上学习教程,这里是链接。
https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_controllers.htm我正在按照其指令创建控制器并从url捕获参数,但是在我运行时显示空白页有时会出现问题有时会显示错误。
“ /”应用程序中的服务器错误。 无法找到该资源。 说明:HTTP404。您正在寻找的资源(或其依赖项之一)可能已被删除,名称更改或暂时不可用。 请查看以下网址,并确保其拼写正确。
要求的网址:/ employee /
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.7.3056.0。
这是我的控制器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCControllerDemo.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Search(string name)
{
var input = Server.HtmlEncode(name);
return Content(input);
}
}
}
这是routeConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Welcome", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Employee", "Employee/{name}", new
{
controller = "Employee", action = "Search", name =UrlParameter.Optional }
);
}
}
}
MapRoute中的Controller和Action名称区分大小写。
因此,员工和员工不匹配。 您要更改传入的URL或将控制器设置调整为:
routes.MapRoute(
"employee", "employee/{name}", new
{
controller = "Employee", action = "Search", name =UrlParameter.Optional }
);
}
}
您必须在默认值之前设置特殊路由。
routes.MapRoute(
name: "Employee",
url: "Employee/{name}",
defaults: new { controller = "Employee", action = "Search", name = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Welcome", action = "Index", id = UrlParameter.Optional }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.