简体   繁体   中英

How do I distribute ASP.NET MVC View as a NuGet Package

I have an ASP.NET MVC View that I use for configuration that I like. I can access that view by navigating to /super-config . I would like to be able to reuse that view across my ASP.NET MVC apps. Ideally, I would like to be able to distribute the view as a NuGet package. Then, if I add the NuGet package to a new ASP.NET MVC app, I could then navigate to /super-config in the ASP.NET MVC app and it would work.

Is there a way to do this in ASP.NET MVC? If so, how?

The ASP.NET Core docs have a page titled Share controllers, views, Razor Pages and more with Application Parts in ASP.NET Core . They have this code snippet:

public void ConfigureServices(IServiceCollection services)
{
    // Requires using System.Reflection;
    // Requires using Microsoft.AspNetCore.Mvc.ApplicationParts;
    var assembly = typeof(MySharedController).GetTypeInfo().Assembly;
    var part = new AssemblyPart(assembly);
    services.AddMvc()
        .ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(part))
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

It's been years since I used the .NET Framework version of ASP.NET MVC and no longer remember how to do it. Searching for two minutes I couldn't find the answer I was looking for. There should be an API where you tell MVC which additional assemblies to search for MVC controllers. But I found this blog post which might help. Of course seconds after I posted this answer, I found a blog post with the info I was looking for immediately below the link to the other blog post I linked. Anyway, this blog post shows how to do it, although I don't recommend at all using Assembly.Load . Use typeof(MyController).Assembly instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM