在MVC 4上更改默认路由

时间:2015-01-09 04:48:40

标签: c# asp.net-mvc asp.net-mvc-4 controller routes

我试图改变我的解决方案的路线,使其从名为" formulariocontroller"的控制器开始。和动作" formulario",我已经在这里查找了一些教程和问题,我认为我做得对,但我不能让它工作,它给了我这个:

enter image description here

这是我的RouteConfig.cs的代码

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

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

            routes.MapRoute(
                "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new
        {controller = "FormularioController",action = "Formulario",id = UrlParameter.Optional}
            );
        }
    }
}

和我的控制者:

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

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Formulario()
        {
            ViewBag.Message = "Modifique esta plantilla para poner en marcha su aplicación ASP.NET MVC.";

            return View();

        }
        [HttpPost]
        public ActionResult Formulario(string text)
        {

            Session["Text"] = text;
            return RedirectToAction("Exito", "Home");
        }

        public ActionResult Exito()
        {


            ViewBag.Message = Session["Text"];

            return View();
        }


    }
}

任何想法?

2 个答案:

答案 0 :(得分:2)

试试这个

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

            routes.MapRoute(
                "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new
        {controller = "Formulario",action = "Formulario",id = UrlParameter.Optional}
            );
        }

将控制器的名称从Formulario

更改为FormularioController

答案 1 :(得分:0)

好吧,我解决了,我改变了文件夹" home"命名为" formulario"并且它有效,这是视图所在的文件夹。

noob错误。

enter image description here