I'm new to MVC but I am fairly good with C#. So I was fiddling with MVC in Visual Studio where I came across this problem and I'm unable locate the cause of this error.

我是MVC的新手,但我对C#相当不错。所以我在Visual Studio中摆弄MVC,我遇到了这个问题,我无法找到导致此错误的原因。

I'm trying to print a variable n number of times. It works very well when i use the given URL

我正在尝试打印变量n次。当我使用给定的URL时,它工作得很好

http://localhost:18665/HelloWorld/Welcome?namelala=Rob&numtimes=3

The output is as expected -

输出如预期 -

This is Rob
This is Rob
This is Rob

However when I try the same with this URL

但是,当我尝试使用此URL时

http://localhost:18665/HelloWorld/Welcome/3/Rob

The output is

输出是

This is Rob

My RouteConfig code :

我的RouteConfig代码:

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


            );

            routes.MapRoute(
                name: "Name_Route",
                url: "{controller}/{action}/{numtimes}/{namelala}"

                );
        }
    }
}

HomeController Code

public ActionResult Welcome(string namelala, int numtimes=1)
        {
            ViewBag.Message = "This is " + namelala;
            ViewBag.numtimes = numtimes;

            return View();
        }

Welcome.cshtml Code

<h2>Welcome here</h2>
<ul>
    @{
        for (int i = 0; i < ViewBag.numtimes; i++)
        {
            <li>@ViewBag.Message</li>
        }

    }
</ul>

What am I doing wrong ?

我究竟做错了什么 ?

1 个解决方案

#1


0

Change your route config to:

将您的路线配置更改为:

routes.MapRoute(
    name: "Name_Route",
    url: "{controller}/{action}/{namelala}/{numtimes}"
);

To match your method signature:

要匹配您的方法签名:

public ActionResult Welcome(string namelala, int numtimes=1)

And also, in line with Stephen's comment, move the route about the default route

而且,根据斯蒂芬的评论,移动关于默认路线的路线

更多相关文章

  1. PHP学习路线图
  2. android 百度地图路线规划去掉节点图标
  3. Android自动化测试学习路线
  4. 【Android】策略模式封装百度地图路线规划模块
  5. 当我运行.jar时,在java.library中会得到一个“No lwjgl”。路线”

随机推荐

  1. 关于android系统Binder机制解析
  2. 深入Android(安卓)'M' Doze
  3. Android(安卓)消息机制之Message
  4. android笔记--android的进程与线程
  5. Android性能分析工具Systrace和TraceView
  6. Android(安卓)App 开源项目使用统计
  7. Android学习笔记19:ImageView实现图片适屏
  8. android 静默安装,含获取各种应用信息方法
  9. Android手机AP模式下本机IP
  10. Android(安卓)Hal 分析