ASP.. NET MVC路由:需要在URL中使用索引,尽管它在默认情况下被声明(使用区域)

本文关键字:情况下 默认 声明 区域 MVC URL 索引 ASP NET 路由 | 更新日期: 2023-09-27 18:10:45

我已经搜索了,但没有解决方案似乎适合我。我有一个web表单VS2013解决方案,我一直在使用一个区域添加MVC。当我指定例如:http://localhost:51961/MVC/contractfiles/index/

时,我已经得到了一切工作

这工作得很好,但我需要它去同一页时,我使用:

http://localhost:51961/MVC/contractfiles/

我得到HTTP错误403.14 - Forbidden - Web服务器被配置为不列出该目录的内容。

My Area registration is:

    namespace PublicHealthCMS_L3.Areas.MVC
    {
        public class MVCAreaRegistration : AreaRegistration 
        {
            public override string AreaName 
            {
                get 
                {
                    return "MVC";
                }
            }
            public override void RegisterArea(AreaRegistrationContext context) 
            {
                context.MapRoute(
                    "MVC_default",
                    "MVC/{controller}/{action}/{id}",
                    new { action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] {"PublicHealthCMS_L3.Areas.MVC.Controllers" }
                );
            }
        }
    }

和我的contractfiles controller:

namespace PublicHealthCMS_L3.Areas.MVC.Controllers
{
    public class ContractFilesController : Controller
    {
        private PublicHealthCMSEntitiesMVC db = new PublicHealthCMSEntitiesMVC();
        // GET: MVC/ContractFiles
        public ActionResult Index()
        {
            int ContractID = MySession.Current.ContractID;
            return View(db.pr_File_Select(242).ToList());
        }

谢谢你的帮助

主路由:

namespace PublicHealthCMS_L3
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "contractfiles", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "PublicHealthCMS_L3.Controllers" }
            );
            //.DataTokens = new RouteValueDictionary(new { area = "MVC" } );
        }
    }
}

全球。asax Application_Start

void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup     
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);            

            //RegisterRoutes(RouteTable.Routes);
        }

我还有一些webapi路由,WebApiConfig.cs:

namespace PublicHealthCMS_L3
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

web . config:

<?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>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="PublicHealthCMS_L3.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <section name="glimpse" type="Glimpse.Core.Configuration.Section, Glimpse.Core" /></configSections>
  <appSettings>
    <add key="RouteDebugger:Enabled" value="false"/>
    <add key="MessageNoContractSelected" value="No contract is selected, please click Search and select a contract" />
    <add key="MessageDeleteRecordFilesExist" value="This record has associated files, please expand the record and remove the files first before the record can be deleted" />
    <add key="Version" value="1.02" />
  <add key="RouteDebugger:Enabled" value="true" /></appSettings>
  <connectionStrings>   
    <add name="PublicHealthCMSConnectionString" connectionString="Data Source=bcvPHDEVSQL;User Id=PublicHealth_User; Password=;Initial Catalog=PublicHealthCMS;" providerName="System.Data.SqlClient" />
    <add name="PublicHealthCMSEntities" connectionString="metadata=res://*/CRMModel.csdl|res://*/CRMModel.ssdl|res://*/CRMModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=BCVPHDEVSQL;initial catalog=PublicHealthCMS;User Id=PublicHealth_User; Password=;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="PublicHealthCMSEntitiesMVC" connectionString="metadata=res://*/Areas.MVC.Models.CMSModel.csdl|res://*/Areas.MVC.Models.CMSModel.ssdl|res://*/Areas.MVC.Models.CMSModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=bcvPHDEVSQL;initial catalog=PublicHealthCMS;user id=PublicHealth_User;password=;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <httpRuntime maxRequestLength="20480" />
    <httpHandlers>
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
    <add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" /></httpHandlers>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <!--<add assembly="System.Data.Entity, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />-->
        <add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
      </assemblies>
      <buildProviders>
        <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </buildProviders>
    </compilation>
    <authentication mode="Windows">
    </authentication>
    <authorization>
      <deny users="?" />
      <!-- This denies access to the Anonymous/unregistered user -->
      <allow users="*" />
      <!-- This allows access to all registered users -->
    </authorization>
    <identity impersonate="false" />
    <pages controlRenderingCompatibilityVersion="4.0"></pages>
  <!-- Glimpse: This can be commented in to add additional data to the Trace tab when using WebForms
        <trace writeToDiagnosticsTrace="true" enabled="true" pageOutput="false"/> --><httpModules>
            <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" />
        </httpModules></system.web>
  <system.webServer>
    <!--<directoryBrowse enabled="true" />-->
    <modules runAllManagedModulesForAllRequests="true"><add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" /></modules>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

      <!-- Your other remove tags-->
      <!--
      <remove name="UrlRoutingModule-4.0" />
       -->
      <!--Your other add tags-->
      <!--
      <add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition="" />-->
    <add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" /></handlers>
    <defaultDocument>
      <files>
        <!--<add value="Search.aspx" />-->
        <add value="Index.cshtml" />
      </files>
    </defaultDocument>
  </system.webServer>
  <system.serviceModel>
    <bindings />
    <client />
  </system.serviceModel>
  <applicationSettings>
    <PublicHealthCMS_L3.Properties.Settings>
      <setting name="PublicHealthCMS_L3_ActiveDirectory_ActiveDirectory" serializeAs="String">
        <value>http://bcvmbiis02/tools/ActiveDirectory.asmx</value>
      </setting>
    </PublicHealthCMS_L3.Properties.Settings>
  </applicationSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
      <!-- 
          For more information on how to configure Glimpse, please visit http://getglimpse.com/Help/Configuration
          or access {your site}/Glimpse.axd for even more details and a Configuration Tool to support you. 
      -->
    </glimpse></configuration>

还有网络。

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="PublicHealthCMS_L3" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <appSettings>
    <!--<add key="webpages:Enabled" value="false" />-->
  </appSettings>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

ASP.. NET MVC路由:需要在URL中使用索引,尽管它在默认情况下被声明(使用区域)

替换此

new { action = "Index", id = UrlParameter.Optional }
与这个:

new { controller="ContractFiles", action = "Index", id = UrlParameter.Optional }

并尝试重建项目。我刚刚验证了,它可以工作

尝试添加到WebConfig文件:

 <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/> 
 </system.webServer>

    <system.webServer>
      <defaultDocument>
       <files>
      <add value="index.cshtml" />
      </files>
     </defaultDocument>
    <directoryBrowse enabled="true" />
    </system.webServer>

请查看来自"IIS未运行ASP "的答案。. NET MVC应用程序"

这是一个奇怪的错误,我已经"解决"它通过添加一个新的控制器和复制和粘贴一切从现有的控制器到这个新的。新的那台很好用。非常令人恼火的是,我花了3-4天时间试图解决一个问题,显然是某种bug,因为新控制器与现有控制器完全相同,但工作正常。

相关文章: