没有采取特殊的路线

时间:2013-12-11 02:39:17

标签: c# asp.net-mvc

我正在尝试创建一个特殊的Report路由,它绕过了web.config中启用的表单身份验证。然而似乎无论我做什么,我总是发送到登录表单,无论我写的什么URL应该与我设置的报告路由匹配。我希望使用此URL localhost / Site / Report / 3

来调用Report controllers Main方法
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("admin/errorlog.axd/{*pathInfo}");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("Scripts/{folder}/{resource}.js.axd/{*pathInfo}");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("XML/{type}/{folder}/{resource}.xml.axd/{*pathInfo}");

        Debugger.Break();
        routes.MapRoute(
            "Report", // Route name
            "Report/{id}", // URL with parameters
            new
            {
                controller = "Report", action = "Main", id = UrlParameter.Optional
            }  // Parameter defaults
        );
        routes.MapRoute(
            "Base", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new
            {
                controller = "Base", action = "Main", id = UrlParameter.Optional
            }  // Parameter defaults
        );
    }

的web.config:

<location path="Views/Report">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>
<system.web>
<httpRuntime maxQueryStringLength="1048576" maxRequestLength="1048576" />
<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>
<compilation targetFramework="4.0" debug="true">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>
</compilation>
<customErrors defaultRedirect="~/Error/ServerError/" mode="On">
  <error statusCode="404" redirect="~/Error/NotFoundError/"/>
</customErrors>
<authentication mode="Forms">
  <forms loginUrl="~/Base/LogOn" timeout="2880"/>
</authentication>
<membership defaultProvider="UserMembership">
  <providers>
    <clear/>
    <add name="UserMembership" type="DAL.Security.UserMembership" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
  </providers>
</membership>
<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"/>
    <add namespace="Resources"/>
  </namespaces>
</pages>
<httpModules>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
  <!--<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />-->
</httpModules>
<httpHandlers>
  <add path="*.js.axd" verb="*" type="Web.ScriptTranslator" />
  <add path="*.xml.axd" verb="*" type="Web.ScriptTranslator" />
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler" />
</httpHandlers>
</system.web>
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxUrl="1073741824" maxQueryString="1073741824" maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
  <validation validateIntegratedModeConfiguration="false"/>
  <defaultDocument>
    <files>
      <add value="Base"/>
    </files>
  </defaultDocument>
  <httpProtocol>
    <customHeaders>
      <clear/>
      <add name="X-UA-Compatible" value="IE=Edge"/>
    </customHeaders>
  </httpProtocol>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler"/>
    <!--<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />-->
  </modules>
  <httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404"/>
    <error statusCode="404" responseMode="Redirect" path="/Site/Error/NotFoundError/"/>
  </httpErrors>
  <handlers>
    <add name="ScriptTranslatorHandler" path="*.js.axd" verb="*" type="Web.ScriptTranslator" preCondition="integratedMode"/>
    <add name="XMLTranslatorHandler" path="*.xml.axd" verb="*" type="Web.ScriptTranslator" preCondition="integratedMode" />
  </handlers>
</system.webServer>

控制器:

public class ReportController : Controller
{

    //
    // GET: /Report/
    [Authorize]
    [HttpGet]
    public ActionResult Main(string id)
    {
        return View("Report", id);
    }
}

1 个答案:

答案 0 :(得分:1)

只要您在控制器中设置了[Authorize] - 属性,如果未经过身份验证,您将始终被重定向到登录页面。删除该属性,它应该工作。