我一直在開發一個站點一段時間,但最近有這個問題,主頁/索引頁面總是重定向到登錄頁面,除非你看不到你已經登錄,Home Controller被[AllowAnonymous] 包圍,我的defulat路線仍然是一樣的。C#-Asp.net MVC主頁/索引頁面重定向到登錄頁面
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我也不是什麼導致這個問題,我以前從來沒有過這樣的,我已經嘗試添加和刪除[使用AllowAnonymous],還是同樣的問題,但奇怪的是指數是唯一在家庭控制器中的操作需要登錄,否則工作正常
[AllowAnonymous]
public class HomeController : Controller
{
public ActionResult Index()
{
try
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
var types = (from r in db.FeedBack
select new FeedBackView { ID = r.ID, Name = r.Name, Jop = r.Jop, Body = r.Body }).Take(7).ToList();
var SlidersList = (from r in db.Sliders
select new SliderView { Description = r.Description, Title = r.Title, ImageURL = r.ImageURL }).ToList();
var FeaturesList = (from r in db.Features
select new FeatureView { Description = r.Description, Title = r.Title, ImageURL = r.ImageURL }).ToList();
var AccordionList = (from r in db.Accordion
select new AccordionView { Description = r.Description, Title = r.Title, ImageURL = r.ImageURL }).ToList();
var ServicesList = (from r in db.Services
select new ServiceView { Body = r.Body, Name = r.Name, ImageURL = r.ImageURL, Glaphicon = r.Glaphicon, ID = r.ID }).Take(6).ToList();
var portfolioTypesList = (from r in db.PortfolioTypes
select new PortfolioTypeView { Name = r.Name, ID = r.ID, filter = r.filter }).Take(5).ToList();
List<PortfolioView> Portfolios = new List<PortfolioView>();
List<PortfolioView> toadd = new List<PortfolioView>();
portfolioTypesList.ForEach(r =>
{
//Portfolios.AddRange((from s in db.Portfolios
// where s.PortfolioTypeID == r.ID
// select new PortfolioView { TypeName = r.Name, ID = s.ID, filter = r.filter, ProjectName = s.ProjectName }
// ).Take(6).ToList());
toadd = (from s in db.Portfolios
where s.PortfolioTypeID == r.ID
select new PortfolioView { TypeName = r.Name, ID = s.ID, filter = r.filter, ProjectName = s.ProjectName }).Take(6).ToList();
Portfolios.AddRange(toadd);
});
var x = new List<PortfolioView>();
PortfolioImage im = new PortfolioImage();
Portfolios.ForEach(r =>
{
im = db.PortfolioImages.Where(s => s.PortfolioID == r.ID).FirstOrDefault();
if (im != null)
{
x.Add(new PortfolioView()
{
ID = r.ID,
TypeName = r.TypeName,
ProjectName = r.ProjectName,
URL = im.ImageURL,
filter = r.filter
});
}
else
{
x.Add(new PortfolioView()
{
ID = r.ID,
TypeName = r.TypeName,
ProjectName = r.ProjectName,
URL = "",
filter = r.filter
});
}
}
);
var Articels = (from e in db.Articles
select new ArticleViewModel
{
Body = e.Body,
Title = e.Title,
ImageURL = e.ImageURL,
ID = e.ID
}).Take(3).ToList();
//var Social = db.Others.FirstOrDefault();
//ViewBag.Social = SocialMedia.convert(Social);
ViewBag.Articels = Articels;
ViewBag.portfolioTypesList = portfolioTypesList;
ViewBag.Portfolios = x;
ViewBag.ServicesList = ServicesList;
ViewBag.AccordionList = AccordionList;
ViewBag.SliderList = SlidersList;
ViewBag.FeaturesList = FeaturesList;
ViewBag.FeedBackGo = types;
return View();
}
}
catch (Exception ex)
{
return RedirectToAction("Error");
}
}
}
控制器具有[授權]屬性?詳細說明你的問題... –
不,控制器沒有[授權]屬性。 –
我認爲向我們展示控制器代碼(或者至少是索引操作)會對您有所幫助 – Sebastiano