c#routing,asp.net mvc。它不接受参数

时间:2018-01-20 19:28:33

标签: c# asp.net-mvc routing

这是我的控制者:

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

namespace MvcMovie.Controllers
{
    public class HelloWorldController : Controller
    {
        // 
        // GET: /HelloWorld/ 
        public ActionResult Index()
        {
            return View();
        }
        // 
        // GET: /HelloWorld/Welcome/ 
        public string Welcome(string name)
        {
            return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: 55 " );
        }
    }
}

这是我的路线:

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 = "Home", action = "Index", id = UrlParameter.Optional }
            );

但每当我通过网址:http://localhost:61114/helloworld/welcome/scott

我的输出是:你好,NumTimes是:55 而不是Hello Scott,NumTimes是:55

1 个答案:

答案 0 :(得分:1)

您的参数名称应为id

您的路线应为{name},name = UrlParameter.Optional