【问题标题】:MVC Redirection to another page result error 404 resource not foundMVC重定向到另一个页面结果错误404资源未找到
【发布时间】:2019-04-26 04:47:48
【问题描述】:

我正在尝试重定向到另一个页面导致错误 404 无法解决

我的查看页面UserDashBoard.cshtml"有代码,

<fieldset >
    <legend > User DashBoard </legend>

    @if(Session["UserName"] != null)
    { <text >
    Welcome @Session["UserName"].ToString() </text>
    }

    @using (Html.BeginForm("GetSurvey", "DashboardController"))
    {
    <input type="submit" value="Some text" />
    }
</fieldset>

我想用控制器DashboardController.cs重定向到页面GetSurvey.cshtml

GetSurvey 就像,

@model List<SelectListItem>

@{
    ViewBag.Title = "GetSurvey";
}

<h2>Survey List</h2>
<fieldset>
    <legend> User DashBoard </legend>

    @using (Html.BeginForm("GetSurvey", "DashboardController", FormMethod.Post))
    {
        @Html.DropDownList("ddlCustomers", Model)
        <br />
        <br />
        <input type="submit" value="Submit" />
    }
</fieldset>

DashboardController 是,

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

namespace Survey.Controllers
{
    public class DashboardController : Controller
    {
        //
        // GET: /Dashboard/

        public ActionResult GetSurvey()
        {
            List<SelectListItem> customerList = Survey();
            return View(customerList);
          //  return View();
        }

        public ActionResult GetSurvey(string ddlCustomers)
        {
            List<SelectListItem> customerList = Survey();
            if (!string.IsNullOrEmpty(ddlCustomers))
            {
                SelectListItem selectedItem = customerList.Find(p => p.Value == ddlCustomers);
                ViewBag.Message = "Name: " + selectedItem.Text;
                ViewBag.Message += "\\nID: " + selectedItem.Value;
            }
            return View(customerList);
        }
        private static List<SelectListItem> Survey()
        {
            SurveyAppEntities ObjectSur=new SurveyAppEntities();
            List<SelectListItem> customerList = (from p in ObjectSur.Surveys.AsEnumerable()  
                                                 select new SelectListItem
                                                 {
                                                     Text = p.Title,
                                                     Value = p.ID.ToString()
                                                 }).ToList();

            return customerList;
        }
    }
}

我不知道为什么它没有重定向到上面提到的页面,因为我在启动项目时遇到了同样的问题,因为我重命名了 HomeController 名称。

路线:

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
    );
}

【问题讨论】:

  • 这是您的路线,请发布您的路线
  • 我已经用我的路线更新了帖子
  • 您的表单使用POST。你的控制器只处理GET

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 model-view-controller


【解决方案1】:

这是检查应用程序中正在发生的事情的简单方法。

互联网上存在一些用于记录 HTTP/HTTPS 流量的工具,例如 fiddler,当浏览到您服务器上的链接时,您可以看到正在发送的数据以及从您的请求的 URL项目。 要使其捕获本地数据,您需要进入您的项目设置,并显示特定的 URL,只需在 localhost 后附加 .fiddler 即可。 例如http://localhost.fiddler:4444/MyTestApp

以上将在调试您的应用程序时为您提供更多数据。

现在,只需从您的视图中的 DashboardController 中删除 Controller 部分,它现在应该指向正确的 URL,也请不要装饰您的 GetSurvey默认隐含使用 [POST] 作为 GET 的方法,您可以通过使用 @Html.BeginForm 方法的重载来覆盖此行为。

@using (Html.BeginForm("someAction", "someController", FormMethod.Get))
{
    ...
}

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2020-07-31
    • 1970-01-01
    • 2017-01-15
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多