samedi 9 avril 2016

Redirect to Login Page if Image not found MVC 4

I am stuck into very strange situation where my MVC 4 Application is throwing at login page(account/login) if image with its directory path is not found in my project.

HTML Helper Method:

public static MvcHtmlString GetTemplateThumgImgPath(this System.Web.Mvc.HtmlHelper helper, string ImageName)
    {
        string staticpath = VirtualPathUtility.ToAbsolute("~/content/templates/");
        return (new MvcHtmlString(staticpath + ImageName));
    }

View:

        <ul>
            @foreach (var o in Model.ObjTemplateList)
            {
                <li>
                    <img src="@Html.GetTemplateThumgImgPath(string.Concat(o.Directory, "/", o.ThumbImage))" width="50px" height="50px" />
                </li>                        
            }
        </ul>

Web Config:

<customErrors mode="On"  defaultRedirect="~/Error/PageNotFound">
  <error statusCode="404" redirect="~/Error/PageNotFound"/>
</customErrors>

Now if the directory + image does not exists in my project then it throws to login page every time i request to any other controller binded with authorize attribute. I have tested it by removing my HTML helper method from image src, then everything works.

Also, Magically when i update Custom error mode to Off then still this issue does not arise. /content/templates/ is my base directory where many template folders can be created at run time.

Is something i missed? Pls help

Aucun commentaire:

Enregistrer un commentaire