Asp.net mvc 无控制器或动作的MVC路由

Asp.net mvc 无控制器或动作的MVC路由,asp.net-mvc,routing,Asp.net Mvc,Routing,考虑我的路线设置: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Question", url: "{number}", defaults: new { c

考虑我的路线设置:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Question",
            url: "{number}",
            defaults: new { controller = "Home", action = "ViewQuestion" }
        );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { id = "\\d+" }
        );
    }
我想要的是:

1) 如果url类似于
myserver/123
,请调用
Home.ViewQuestion(字符串编号)

2) 否则,使用
Home/Index
默认操作在控制器和操作中搜索

我现在在请求myserver/123时得到的是:

The view '123' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/123.aspx
~/Views/Home/123.ascx
~/Views/Shared/123.aspx
~/Views/Shared/123.ascx
~/Views/Home/123.cshtml
~/Views/Home/123.vbhtml
~/Views/Shared/123.cshtml
~/Views/Shared/123.vbhtml
我的行动和看法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdvancedWebApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ViewQuestion(int? number)
        {
            return View(number + "");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
ViewQuestion.cshtml:

@model string
@{
    ViewBag.Title = "ViewQuestion";
}

<h2>ViewQuestion: @Model</h2>
@模型字符串
@{
ViewBag.Title=“ViewQuestion”;
}
视图问题:@Model
使用url:“ViewQuestion/{number}”代替url:“{number}”,并使用您的操作代码:

public ActionResult ViewQuestion(int? number)
    {
        //your code
    }

原来我在调用controller-helper方法
View(string-viewName)
,就好像它是
View(对象模型)

    public ActionResult ViewQuestion(string questionNumber)
    {
        return View((object)questionNumber);
    }

从控制器的操作来看,似乎您正在返回视图名称作为传递给它的编号。因此,MVC运行时正在寻找123.html123.aspx,而您已经将视图定义为ViewQuestion.cshtml

public ActionResult ViewQuestion(int? number)
    {
        return View(number + "");
    }
您需要返回正确的视图名称,如下所示

public ActionResult ViewQuestion(int? number)
    {
        return View("ViewQuestion",number);
    }

请确保您的控制器具有操作视图问题。另外,方法的输入参数必须命名为number。它在本地为我工作,因为它是由您提供的。@dotnetstep我已经创建了一个新项目,将代码粘贴到其中,但仍然不工作。您对问题路径有任何限制吗?如果是,请检查它是否有效。(根据这张图片,我看到约束与值有一些问题)谢谢。我需要直接进入我的网站,然后是NumberRok,还有一种方法。在RouteConfig.cs中,添加此行routes.mapmvCattributerRoutes();在routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”)行之后;然后用[Route(“{number}”)]装饰您的操作方法(ViewQuestion),例如,根据您的需要,将调用ViewQuestion操作。它会有用的。在您上传的图像中,我看不到url。如果您按照我上面提到的步骤操作,它将起作用。为了避免调用控制器帮助器方法视图(字符串viewName),您可以将其保存在viewbag.QUOTESNUMBER=QUOTESNUMBER这样的视图包中,并在视图中显示它-@viewbag.QUOTESNUMBER,而不是从View函数传递(对象)QUOTESNUMBER。