WebActivator.PreApplicationStartMethod does not work

前端 未结 5 1173
-上瘾入骨i
-上瘾入骨i 2020-12-15 23:34
[assembly:  WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.StructureMapMvc), \"Start\")]

namespace MyApp.App_Start
{
    public static class Structur         


        
相关标签:
5条回答
  • 2020-12-15 23:43

    If your code is in a Web Site Project (ie, under the App_Code folder) you cannot use PreApplicationStartupMethod! You can use PostApplicationStartupMethod instead. The "Pre" method executes before global.asax *Application_Start* runs, while "Post" executes after.

    I wasted a good hour or two before I figured this out, so hopefully this will help someone else avoid that!

    0 讨论(0)
  • 2020-12-15 23:50

    My experience is that WebActivator will not work if your project settings (in .csproj.user or .vbproj.user) have the setting <StartAction>NoStartPage</StartAction> the fix is to set it to <StartAction>CurrentPage</StartAction> and it should then work next time you debug.

    Also since it's in a .user file (which are typically not included in svn) it is difficult to determine why it works on some dev environments but not others.

    0 讨论(0)
  • 2020-12-15 23:50

    My problem was twofold.

    1) not declaring full path to the type

    2) placing attribute inside the namespace, not before

    Once I fixed both of these it worked.

    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyApp.Api.Controllers.MyController), "AutoMapperStart")]
    
    
    namespace MyApp.Api.Controllers
    {
    
        public class MyController : ApiController
        {
            public static void AutoMapperStart()
            {
                MyMapperConfig.DefineMappings();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 23:51

    For me, the problem I had was when creating a private NuGet repository using NuGet.Server download (it makes use of WebActivatorEx PreApplicationStartMethod attribute).

    What I did was create an Empty Web Site project. This is incorrect: it needs to be an Empty Web Application project. Once I created the Empty Web Application and re-installed NuGet.Server everything worked fine.

    So: If you've used an Empty Web Site project, that might be why you're having the problem. Use an Empty Web Application project instead.

    I'm thinking the Empty Web Site project is missing some of the ASP.NET "glue" which enables the System.Web.PreApplicationStartMethod (as used by WebActivatorEx) to work. Maybe someone who knows a little more of the details could explain why this is?

    0 讨论(0)
  • 2020-12-16 00:01

    With WebActivator version 1.5.3, the MyClass.cs.pp file cannot just live in the App_Start folder, but must live in the content\App_Start folder in order for nuget install to create the transformed file in the App_Start of the target project.

    This is undocumented, as far as I can tell.

    NOTE: This solution seems to work so long as the originating .nupkg was built using nuget pack using a conventional file system approach, but NOT when using nuget pack that targets a specific .csproj file.

    0 讨论(0)
提交回复
热议问题