Project empty MVc. I can not start

2

I started doing a project from scratch using MVC 4 of VS 2012. As I opted for an empty, I can not start the view. I made the route:

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

But when I give an F5, it gives me this error:

Erro de Servidor no Aplicativo '/'.

Não é possível encontrar o recurso. 
  Descrição: HTTP 404. O recurso que você está procurando (ou uma de suas dependências) não pôde ser removido, seu nome foi alterado ou está temporariamente indisponível. Examine o URL e certifique-se de que está digitado corretamente. 

 URL solicitada: /


Informações sobre a Versão: Microsoft .NET Framework Versão:4.0.30319; Versão do ASP.NET:4.0.30319.34249 

In my URL that is: http://localhost:60975/ I add after the slash (/) the controller name and still giving me error that did not find the view index. How do I resolve this?

public class OperadorController : Controller
    {
        //
        // GET: /Operador/

        public ActionResult Index()
        {
            return View();
        }

    }

View

@{
    ViewBag.Title = "Operador";
}

<h2>Operador</h2>

Package.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AutoMapper" version="4.0.4" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="2.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="4.5.6" targetFramework="net45" />
</packages>

Global.asax

// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }

Route.config

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

        routes.MapRoute(
            name: "Operador",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Operador", action = "Index", id = UrlParameter.Optional }
       );
    }
}
    
asked by anonymous 07.10.2015 / 22:36

2 answers

3

I know you've answered your own question, but wanted to expand a little more. As you've noticed, MVC uses a lot conventions . But there are ways to make out of the conventions and do whatever you want.

In your case, the convention is that you have to have a view with the same name as the action that is in the controller. For example:

Controller.cs

public ActionResult AlgumaCoisa()
{
  return View();
}

So, by convention, you would have to create a view with the same name:

SomeCase.cshtml

@{ ViewBag.Title = "Alguma Coisa" }
<h2>Alguma coisa está aqui</h2>

It's also good to know that views that exist in other folders can also be used:

Controller.cs

public ActionResult AlgumaOutraCoisa()
{
  return View("AlgumaCoisa/AlgumaOutraCoisa");
}

And your view:

/AlgumaCoisa/AlgumaOutraCoisa.cshtml

@{ ViewBag.Title = "Alguma outra coisa." }
<h2>Alguma outra coisa está aqui.</h2>
    
08.10.2015 / 04:28
1

I already know what happened. There is no view index, so it wants to return this view. I did so in my controller and resolved:

public ActionResult Index()
        {
            return View("Operador");
        }
    
07.10.2015 / 23:49