Showing posts with label MVC Razor. Show all posts
Showing posts with label MVC Razor. Show all posts

Tuesday, 10 July 2012

Could not load type 'System.Web.Http.Dependencies.IDependencyScope'

I am working on one Asp.Net WebApi project along with 5 members in my team. We are using
Asp.net MVC4  Beta for the WebApi project. Everything is working for me fine. Suddenly one day I got the latest version of code from TFS and try to run the application. The solution build succeed, but the application throwing the following exception.
Could not load type 'System.Web.Http.Dependencies.IDependencyScope' from assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
The same application is running in my other teammates. I researched around one day, but did not found any solution that why this dll is not loading run time even though the dll is present in the bin folder.  One of my team mate told that Asp.Net MVC4 is updated from beta to release version. I updated that in my system then the application is working fine. There was mismatch in the team communicatin about this updatation I lost my one day. If any one get this exception try to update the WebApi to release candidate.

Monday, 20 February 2012

How to get the current controller and action names in View Mvc3

In MVC we can use the partial views to avoid the redundant code. I have one requirement that the action names are same in two different controllers and there is partial view which has one link which needs to point to the current controller with the same action. I want to access the controller name in View to avoid the redundant code. The solution to get the Controller and Action names in View as follows. Solution:
 @{
        var controller = this.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
    
        Change password
        }
If you want to access the action name
var action=this.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();

Jquery Ajax Request and Unauthorized Access handling in MVC3

When we are posting the data through jquery $.ajax and  we are using asp.net forms authentication, if the user is not authorized then we will redirect to the login page. the same login page response is return result as on jquery ajax success. If we throw any exception in controller or any custom authorize attribute then ajax request returns the 500 status code internal server exception as its default behaviour. 500 status code reruns any exception in the request processing. But our view is to return the 401 as the status code. I research about many hours to get this solution.

Solution1:

1. Custom authentication attribute
public class AdminAuthorizeAttribute : AuthorizeAttribute  
{   
 protected override bool AuthorizeCore(HttpContextBase httpContext)
 {    
  User currentUser = ClientContext.Current.User;    

  return currentUser != null;      

 }

}
If the user is not authenticated the reruns to logon action as i am using forms authentication in asp.net.
2. In the controller action result write the following
 public ActionResult Logon()
 {
  if (Request.IsAjaxRequest())
  {
 
  //this is used when the authentication fails in the ajax request
  //this returns the httpstatus code 401 to the browser.
 
   ControllerContext.HttpContext.Response.StatusCode = 401;
 
   return Content(string.Empty);
  
   }

 } 
3. Handle the status codes in jquery $.ajaxSetup
$.ajaxSetup({
             statusCode: {
                        401: function () {
 
                            // Redirect the to the login page.
                            window.location = "/login/";
 
                        }
                    }
            });
Solution2:
public class AdminAuthorizeAttribute : AuthorizeAttribute  
{   
 protected override bool AuthorizeCore(HttpContextBase httpContext)
 {    
  User currentUser = ClientContext.Current.User;    

  return currentUser != null;      

 }


public override void OnAuthorization(AuthorizationContext filterContext)
{
   base.OnAuthorization(filterContext);

  // If its an unauthorized/timed out ajax request go to top window and redirect to logon.
    if (filterContext.Result is HttpUnauthorizedResult && filterContext.HttpContext.Request.IsAjaxRequest())
    {
filterContext.Result = new JavaScriptResult() { Script = "top.location.reload()"    };
    }

  // If authorization results in HttpUnauthorizedResult, redirect to error page instead of Logon page.
            if (filterContext.Result is HttpUnauthorizedResult)
            {
                if (ClientContext.Current.User == null)
                {
                    filterContext.Result =
                        new RedirectResult(
                            string.Format(
                                "~/logon/logon?ReturnUrl={0}",
                                HttpUtility.HtmlEncode(filterContext.HttpContext.Request.RawUrl)));
                }
                else
                {
                    filterContext.Result =
                       new RedirectResult(string.Format("~/error/unauthorized"));
                }
            }
        }
}
I hope this will help you. 

Thursday, 14 April 2011

How to run MVC3 Razor Application on IIS6

IIS6 Extension-less URLs

All the MVC3 razor application are not having any extension files. The IIS6 is not understand the extension less urls by default. In order to solve this problem the solution is as follow:

The extension-less URLs using the infamous “Star mapping” or “Wildcard mapping” feature of IIS 6. I say infamous because there is a lot of concern over the performance implications of doing this. Of course, you should measure the performance of your site for yourself to determine if it is really a problem.

Steps
  1. Go to Run-->Inetmgr-->then expand your website-->select virtual directory-->righclick-->properties.
  2. Click on Configuration-->Application Configuration Properties dialog will open.
  3. Select .aspx in application fileextensions--->clik on Edit. then Add/edit application extension mappind dialog will open.
  4. Copy the executable text-->click on cancel.
  5. Go back to Application Configuration dialog-->Click on inset beside the Wild card application maps
  6. then paste the executable text which is copied previously(that is nothing but isapi filter file path). Uncheck verify that file exits. dont forget to uncheck Verify that file exists. Click on ok.
  7. Restart iis.
  8. Now run your application.

Tuesday, 12 April 2011

Getting jQuery Intellisense in Razor pages


Get jQuery Intellisense by adding this to a page:
@if(false)
{
<script src="../../Scripts/jquery-1.4.2-vsdoc.js" language="javascript" />
}