我有一个在 MVC5 .Net 4.5.2 中有一些视图的项目然后我在解决方案中添加了第二个 MVC 项目,它使用 razorgenerator nuget 包来预编译视图以便在第一个项目中使用。接下来,我使用添加到 GlobalFilters 的自定义 GlobalFilterCollection 将用户引导至第一个项目中的视图或第二个项目中的特定视图。
当在 VS2013 (IIS Express) 中本地运行时,此方案效果很好。但是,当我将它放在服务器上(Win server 2008,IIS7)时,它在被 javascript 调用时找不到视图,只有最初调用的视图。
有任何想法吗?
定义的路线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "Index", id = UrlParameter.Optional }
);
使用的过滤器:
override public void OnActionExecuting(ActionExecutingContext filterContext)
{
else
{
string requestedAction = (filterContext.ActionDescriptor).ActionName;
string requestedController = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
//If we are already going to the controller action which serves the login view when not logged in, then ignore the filter:
if ((requestedAction == "Login" || requestedAction == "LoginAction") && requestedController == "DBLogin")
{
base.OnActionExecuting(filterContext);
return;
}
var Session = filterContext.HttpContext.Session;
//If we got here, then another page was requested, so verify the login and change the route if necessary:
if (Session["IsLoggedIn"] != null && (bool)Session["IsLoggedIn"] == true)
{
base.OnActionExecuting(filterContext);
return;
}
else
{
RouteValueDictionary reRouter = new RouteValueDictionary();
reRouter.Add("action", "Login");
reRouter.Add("controller", "DBLogin");
filterContext.Result = new System.Web.Mvc.RedirectToRouteResult(reRouter);
}
}
}
调用 Javascript 以请求在 IIS 上失败的后续视图:
$.support.cors = true;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: SERVER_TO_USE + "/DBLogin/LoginAction",
data: parameters,
success: function (data, textStatus, jqXHR) {
callBackLoginSuccess(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
hasError = true;
lastErrorMessage = errorThrown;
},
complete: function (jqXHR, textStatus) {
if (hasError) {
var errorObject = ErrorHandling.BuildClientSideError(lastErrorMessage);
ErrorHandling.ShowErrors(errorObject);
}
},
dataType: 'json'
});