0

控制器:

public class HomeController : Controller
{
    Models.MakaleSitesiDBEntities entity = new Models.MakaleSitesiDBEntities();

    public ActionResult ArticlesByCategory(int CategoryId)
    {
        IEnumerable<Models.TableArticles> articles = entity.TableArticles.Where(a => a.CategoryId == CategoryId && a.IsActive == true).OrderBy(a => a.PublishedOn);

        return View(articles.Reverse());
    }

    public ActionResult ArticleDetails(Guid ArticleId)
    {
        if (Session["IsUserRead"] == null || (Guid)Session["IsUserRead"] != ArticleId)
        {
            Session["IsUserRead"] = ArticleId;
            Models.TableArticles article = entity.TableArticles.Where(a => a.ArticleId == ArticleId).SingleOrDefault();
            article.ViewCount++;
            entity.SaveChanges();
        }

        return View(entity.TableArticles.Where(a => a.ArticleId == ArticleId).SingleOrDefault());
    }
}

ArticlesByCategory 允许匿名用户,但 ArticleDetails 不允许。它将页面路由到登录页面?

全球.asax

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MakaleSitesi
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

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

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        // Use LocalDB for Entity Framework by default
        Database.DefaultConnectionFactory = new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        BundleTable.Bundles.RegisterTemplateBundles();
    }
}
}

为什么会这样?

4

2 回答 2

2

对此行为的一种可能解释是,请求正在命中其他一些用[Authorize]属性修饰的控制器操作,而不是ArticleDetails操作。这可能是因为您可能已经完成的一些自定义路由设置无法按预期工作。

另一种可能的解释是,您在 IIS 或您使用的任何 Web 服务器中进行了一些配置,这些配置拒绝匿名访问给定的 url。

于 2012-06-27T08:52:08.007 回答
0

很可能是 Web.config 中身份验证或授权部分中的设置,以下将产生此行为:

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/Login/Index" defaultUrl="/Home" timeout="60" />
  </authentication>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

<location path="home/ArticlesByCategory">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>
于 2012-06-27T08:52:46.133 回答