andrewdavey / postal

Email sending for asp.net mvc using the view engine system to render emails.
http://aboutcode.net/postal
MIT License
536 stars 168 forks source link

The virtual path '/' maps to another application, which is not allowed #142

Open jmsolutionsgroup opened 8 years ago

jmsolutionsgroup commented 8 years ago

Just starting to use this library I get this exception when trying to send a basic email using Asp.Net MVC 5:

Nothing fancy, Just followed the instructions on the website. I am running my application as http://localhost/sitename. If I change my url to be http://localhost/ the email works without an issue This is the same issue as https://github.com/andrewdavey/postal/issues/65

[ArgumentException: The virtual path '/' maps to another application, which is not allowed.] System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) +12796283 System.Web.HttpContext.GetFilePathData() +60 System.Web.HttpContext.GetRuntimeConfig() +38 System.Web.HttpRequest.get_Browser() +191 System.Web.HttpRequestWrapper.get_Browser() +18 System.Web.WebPages.BrowserHelpers.GetOverriddenBrowser(HttpContextBase httpContext, Func2 createBrowser) +360 System.Web.WebPages.DisplayModeProvider.<.ctor>b2(HttpContextBase context) +11 System.Web.WebPages.d4.MoveNext() +192 System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) +856 System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) +206 System.Web.Mvc.<>cDisplayClass6.b4(IViewEngine e) +43 System.Web.Mvc.ViewEngineCollection.Find(Func2 lookup, Boolean trackSearchedPaths) +174 System.Web.Mvc.ViewEngineCollection.Find(Func2 cacheLocator, Func2 locator) +23 Postal.EmailViewRenderer.Render(Email email, String viewName) +63 Postal.EmailService.Send(Email email) +58 UHearth.Web.Controllers.<>c__DisplayClass16.<SendEmailConfirmation>b__15() in c:\Users\Remco\Documents\Visual Studio 2013\Projects\HearthstoneTracker.com\UHearth.Web\Controllers\AccountController.cs:186

jmsolutionsgroup commented 8 years ago

I want to clarify my issue. I am utilizing Hangfire to send emails in the background. I have followed the tutorial located http://docs.hangfire.io/en/latest/tutorials/send-email.html. When I have IISExpress configured to debug the site on http://localhost:7706 everything works correctly. When I have IISExpress configured to debug the site on http://localhost:7706/SubSite I receive the error

The virtual path '/' maps to another application, which is not allowed

I have tracked the error down to the following line in the EmailViewRenderer.cs file

            var result = viewEngines.FindView(controllerContext, viewName, null);

of

        IView CreateView(string viewName, ControllerContext controllerContext)
    {
        var result = viewEngines.FindView(controllerContext, viewName, null);
        if (result.View != null)
            return result.View;

        throw new Exception(
            "Email view not found for " + viewName +
            ". Locations searched:" + Environment.NewLine +
            string.Join(Environment.NewLine, result.SearchedLocations)
        );
    }
jmsolutionsgroup commented 8 years ago

I found that the issue was that the controllerContext httpContext was not getting set correctly. In the method

    string UrlRoot()
    {
        var httpContext = HttpContext.Current;
        if (httpContext == null)
        {
            return "http://localhost";
        }

        return httpContext.Request.Url.GetLeftPart(UriPartial.Authority) +
               httpContext.Request.ApplicationPath;
    }

if the httpContext is null which is the case when using hangfire the default value is "http://localhost"

If I changed the default value to be "http://localhost/SubSite" then everything will work without an issue. Is there a configuration that I can set to get this to work without modifying the source code?

mauricioulloa commented 8 years ago

I'm having the same issue. Is there any solution that does not involve modifying the source code??

jmsolutionsgroup commented 8 years ago

I was able to resolve my issue, I had previously posted on stackoverflow. Here is the link to my issue and resolution. http://stackoverflow.com/questions/35389505/using-postal-and-hangfire-in-subsite

mauricioulloa commented 8 years ago

@uchetfield Thanks!

jmsolutionsgroup commented 8 years ago

@mauricioulloa FYI I updated my answer on stackoverflow with a more elegant solution that also resolved some other issues that appeared once my application was a heavy stress.