Project Empty Mvc. I can’t startar

Asked

Viewed 108 times

2

I started doing a project from scratch using MVC 4 from VS 2012. As I opted for an Empty, I can’t get the view started. I took 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 that mistake:

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 it is: http://localhost:60975/ i add after the bar(/) the controller name and keep giving me error that did not find the view Index. How do I fix this?

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

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

    }

View

@{
    ViewBag.Title = "Operador";
}

<h2>Operador</h2>

O 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);
        }
    }

The 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 }
       );
    }
}
  • MeuController exists?

  • @Gypsy omorrisonmendez, yes he exists and created a view for him. I’m just not sure is how to call him.

  • Weird. You can put the Controller and View in your question, please?

  • I edited the question

  • You can also put the packages.config on the question? Apparently everything is right with the structure.

  • It’s also okay. I want to check the Global.asax.cs, if possible.

  • It’s all right. I don’t know what else it could be.

  • @Ciganomorrisonmendez, I click with the right button on top of my Action and I give Go to View and I get the following message: Unable to find matching view. I deleted the view and I reset it and it’s still the same thing, which means he can’t find the view, now I don’t know why.

Show 3 more comments

2 answers

3


I know you already answered your own question, but I wanted to expand a little more. As you have noticed, MVC uses a lot conventions. However, there are ways to do outside the conventions and do whatever you want.

In your case, the convention is that you have to have a view with the same name the action 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:

Algumacoisa.cshtml

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

It is also good to know which views 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>
  • brazilianldsjaguar, good morning. So, as I created an Empty project, I thought it was really Empty. When I created the controller, it already created an action called Index(skeleton only) and in Route.config, the same thing. I think that was what was happening and you answered me very well, I even marked your answer for having more consistency and plausible explanation. I understood what was happening.

  • Thanks! MVC has caught me a few times too.

1

I already know what happened. There is no view Index, so he wants to return this view. I did so in my controller and solved:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.