开发者

MVC app not working on server but is fine on development machine

开发者 https://www.devze.com 2022-12-26 16:16 出处:网络
I a开发者_如何学Pythonm able to run my application just fine on my dev machine but as soon as I publish it to the web server some functionality is lost.Any type of .ajax POST does not work usually wit

I a开发者_如何学Pythonm able to run my application just fine on my dev machine but as soon as I publish it to the web server some functionality is lost. Any type of .ajax POST does not work usually with a 401 unauthorized error.

The server is Windows 2008 with IIS7. I also installed Visual Studio 2008 with MVC2 on the server and ran the application directly on the server and the same .ajax POST does not work but the error changed to 500 unknown.

Thanks for the help.


I think you need a Custom ASP.NET MVC Authorization Attribute For Ajax Requests

public class CustomAuthorizeAttribute : AuthorizeAttribute
   {
       public override void OnAuthorization(AuthorizationContext filterContext)
       {
           base.OnAuthorization(filterContext);
           if (filterContext.Result == null || (filterContext.Result.GetType() != typeof(HttpUnauthorizedResult)
               || !filterContext.HttpContext.Request.IsAjaxRequest()))
               return;

           var redirectToUrl = "/login?returnUrl=" + filterContext.HttpContext.Request.UrlReferrer.PathAndQuery;

           filterContext.Result = filterContext.HttpContext.Request.ContentType == "application/json"
               ? (ActionResult)
                 new JsonResult
                 {
                     Data = new { RedirectTo = redirectToUrl },
                     ContentEncoding = System.Text.Encoding.UTF8,
                     JsonRequestBehavior = JsonRequestBehavior.DenyGet
                 }
               : new ContentResult
               {
                   Content = redirectToUrl,
                   ContentEncoding = System.Text.Encoding.UTF8,
                   ContentType = "text/html"
               };

           //Important: Cannot set 401 as asp.net intercepts and returns login page
           //so instead set 530 User access denied            
           filterContext.HttpContext.Response.StatusCode = 530; //User Access Denied
           filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;            
       }
   }


if you are using jquery, are you sure you are requesting data that is on the same domain as your app? If you aren't there are workarounds on this jQuery Forum post:


I had this problem once. When you call your ajax services, be sure to use relative urls.

www.mysite.com/myservice

is not the same as

mysite.com/myservice

when calling from

www.mysite.com

So, what you should use when calling the service is:

/myservice


Answer: It looks like i found my problem.

In my Controller I have the following method:

 [JsonFilter(Param = "course", JsonDataType = typeof(EmployeeSearchItem))]
    public ActionResult ViewCourseByID(EmployeeSearchItem course)
    { 

...}

And EmployeeSearchItem is a class in its own .cs file:

 namespace EducationAssistanceMVC.Models
{
public class EmployeeSearchItem //: Controller
{
    public string empIDSearch { get; set; }

...}

My solution was to comment out the inheritance of Controller above in the EmployeeSearchItem.cs file and also comment out any Using directives.

Wow, I can't believe it took so long to figure that out.

0

精彩评论

暂无评论...
验证码 换一张
取 消