I have the reference, but when I run the CLR says the reference is missing

Asked

Viewed 1,724 times

4

I have this include in my project: System.Web.MVC.Ajax. However when running the application I get an error Runtime, in the archive web.config. If I comment on the error line on web.config, error appears in the bottom line, referring to the System.web.MVC.Html. This is the error that appears:

Server Error in Application '/'.

Build Error

Description: Error when compiling a resource needed to meet this request. Examine the specific details of the error and modify the source code appropriately.

Compiler Error Message: CS0234: The namespace name or type 'Ajax' does not exist in the 'System.Web.Mvc' namespace. You need a Assembly reference?

Error of Origin:

Line 22: Line 23:
Line 24: //here gives the error. Line 25:
Line 26:

Source File: c: Projects_mvc Sistema.Mvc Web.config Line: 24

Show Compiler Detailed Output:

Show Full Build Source:

Version Information: Microsoft . NET Framework Version:4.0.30319; Version of ASP.NET:4.0.30319.34249

What I don’t understand is that I have the reference and see the includes of my controller and none is with errors:

using System.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

Well, this is my route file, might be useful:

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

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

And this is my view that I’m starting to create:

@{
    ViewBag.Title = "Index";
}

<h2>Index - Teste de Partial View</h2>

In Viewbag is giving the following error.

Viewbag do not exist in the Current context

I just don’t understand what’s really going on.

Below my package.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AutoMapper" version="4.0.4" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.3" 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>

The web.config file

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <httpRuntime targetFramework="4.5" />
    <compilation debug="true" targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>
  • How’s your packages.config?

  • I see the installation of each package of your MVC application.

  • This is the second time you have asked me about it. It has to do with references too?

  • Yes, totally, but references are ok. I need your file now web.config.

1 answer

1

Its configuration is quite wrong. An ASP.NET MVC application has two files Web.config, being one inside the directory Views and one in the root directory. What you did was mix the two.

This needs to be removed:

    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>

This section does not belong to the <system.web> of web.config root, and yes, to tag <system.web.webPages.razor> of web.config of the directory Views.

A correct configuration would be something like:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Note that this setting is for MVC5, not MVC4, which should be your case. Make settings carefully.

  • I just got back from lunch and I’ll do it, but I didn’t do anything to suggest a mix of web.config. The system itself did this I think, maybe when creating my view and some partial view, it might be, but I didn’t copy anything or I might have done when inserting or deleting some references, but this is every programmer’s day-to-day.

  • If I do this, it gives the following error: Description: HTTP 404. The resource you are looking for (or one of its dependencies) could not be removed, its name has been changed or is temporarily unavailable. Scan the URL and make sure it is typed correctly. URL requested: /

Browser other questions tagged

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