How to enable bundling in ASP.NET MVC 5 - asp.net-mvc-5

I was playing a little bit to prevent bundling files and now I am stuck at how to return them to be a bundled.
I tried to set debug=false, also I entered BundleOptimization to true and I have files separated.
My BundleCOnfig.cs looks like:
public class BundleConfig
{
// For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
BundleTable.EnableOptimizations = true;
#region Styles bundles
var bundlesCSS = new StyleBundle("~/bundles/css")
.Include("~/Content/css/libs/bootstrap/bootstrap.css")
//.Include("~/Content/css/libs/fontawesome/font-awesome.css")
.Include("~/Content/css/libs/camera/camera.css")
.Include("~/Content/css/libs/fontawesome/font-awesome.css", new CssRewriteUrlTransformWrapper());
var bundlesCustom = new StyleBundle("~/bundles/css/custom")
.Include("~/Content/css/custom/general.css")
bundlesCSS.Orderer = new AsIsBundleOrderer();
bundlesCustom.Orderer = new AsIsBundleOrderer();
bundles.Add(bundlesCSS);
bundles.Add(bundlesCustom);
bundles.Add(new StyleBundle("~/bundles/hotel-datepicker-css").Include(
"~/Content/css/libs/baguetteBox.min.css",
"~/Content/css/hotel-datepicker/hotel-datepicker.css"
));
#endregion
}
}
and my web.config looks like this:
<?xml version="1.0"?>
<system.web>
<compilation debug="false" targetFramework="4.7.2"/>
<httpRuntime targetFramework="4.7.2"/>
<httpModules>
<add name="TelemetryCorrelationHttpModule"
type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation"/>
</httpModules>
</system.web>

Have you added below line of code in Application_Start() of Global.asax.cs file?
BundleConfig.RegisterBundles(BundleTable.Bundles);
if yes, then BundleTable.EnableOptimizations = true enables bundling and minification in debug mode. If you set it to false then it will not do bundling and minification.

Related

Partial platform specific methods in .NET MAUI

I'm trying to implement plattform specific partial method in .NET MAUI to get the connection string for the database.
In the "main application":
namespace TestApp.DL;
public partial class BaseHandler
{
public partial string GetDBPath();
private string GetCnnPath()
{
var dbPath = GetDBPath();
var cnnPath = $"Data Source={dbPath}";
return cnnPath;
}
...
}
in the platform folders in the project:
where each contain the plattform specific implementation:
namespace TestApp.DL;
// All the code in this file is only included on Android.
public partial class BaseHandler
{
public string GetDBPath()
{
var dbName = "com.mycompany.mydatabase.db";
return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
}
}
...but I keep getting "Error CS8795: Partial method 'BaseHandler.GetDBPath()' must have an implementation part because it has accessibility modifiers. (CS8795)". It seems like the platform specific files are not seen by the compiler? Note, they are in a separate assembly project from the main application but that should be ok I guess, given that the fwk created the folders for me?
When you struggle with partials you can keep using partial classes, but avoid using partial methods. This is especially true when creating maui libs, were this approach tends to break, while in maui apps the compilation works fine.
The "quick fix solution", all partial classes must use same namespace obviously:
Shared code, you would want to change NET6_0 to NET7_0 whatever you are using:
public partial class BaseHandler
{
private string GetCnnPath()
{
var dbPath = GetDBPath();
var cnnPath = $"Data Source={dbPath}";
return cnnPath;
}
#if (NET6_0 && !ANDROID && !IOS && !MACCATALYST && !WINDOWS && !TIZEN)
public string GetDBPath()
{
throw new PlatformNotSupportedException();
}
#endif
}
Your platform specific code in platform Platforms/Android:
public partial class BaseHandler
{
public string GetDBPath()
{
var dbName = "com.mycompany.mydatabase.db";
return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
}
}
First of all, your implementation must use the partial keyword as well:
namespace TestApp.DL;
// All the code in this file is only included on Android.
public partial class BaseHandler
{
public partial string GetDBPath()
{
var dbName = "com.mycompany.mydatabase.db";
return Android.App.Application.Context.GetDatabasePath(dbName).AbsolutePath;
}
}
Then, you should make sure that you're following the guidelines for multi-targeting: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/configure-multi-targeting#configure-folder-based-multi-targeting
You'll need to update your .csproj file with the following:
<!-- Android -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-android')) != true">
<Compile Remove="**\Android\**\*.cs" />
<None Include="**\Android\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<!-- iOS -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-ios')) != true">
<Compile Remove="**\iOS\**\*.cs" />
<None Include="**\iOS\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<!-- Mac Catalyst -->
<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-maccatalyst')) != true">
<Compile Remove="**\MacCatalyst\**\*.cs" />
<None Include="**\MacCatalyst\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<!-- Windows -->
<ItemGroup Condition="$(TargetFramework.Contains('-windows')) != true">
<Compile Remove="**\Windows\**\*.cs" />
<None Include="**\Windows\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
Without this, you would see another compiler error because you can only provide one body for any partial method declaration. You need to provide platform specific implementations for each platform and disable the compilation of the ones that are not needed or you can add a default implementation, but then you need file-based multi-targeting instead of platform-based multi-targeting (or a combination of both).
I've had a similar problem already and solved it here: MAUI: How to use partial classes for platform specific implementations together with net7.0 as TargetFramework in the SingleProject?

Blazor WASM hosted project on IIS : 404 errors when DLL are loaded

My app is a blazor app, web assembly and hosted.
I installed IIS in windows, and added a site named PBM.
Then, I published my app with webassembly, here are some parameters:
(it says the deployment mode is framework dependant, and the target runtime is 'portable').
Here is the directory structure in IIS:
The errors I get are 404 while the app is loading, and especially when the DLL are loaded: (here is the debug windows of chrome)
The error of integrity seems as I read somewhere not the origin of the problem, its real origin is the 404 (not found) error.
I saw that there is no _framework directory in the PBM folder. Is this normal? There is one in wwwroot(is seems to be the one of the client project?. But there are DLL in the PBM folder:
Please notice I changed the index.html (usually located in the wwwroot directory of the client project)to index.cshtml (in the Pages directory of the Server project), in order to use multiple configurations.
Can you tell me how to fix these 404 errors with the DLLs?
thank you.
EDIT
#Lex Li: here is my web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\Application.Server.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
It seems there is no particular change to default file types.
EDIT
#Just the benno: here is my Startup.cs file:
public class Startup
{
public string ConnectionString { get; set; }
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
services.AddDbContext<DB>(options => {
options.UseSqlServer(Configuration.GetValue<string>("Configuration:Connection"));
});
services.AddScoped<DB>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Tools.ConnectionString = Configuration.GetValue<string>("Configuration:Connection");
app.UsePathBase(Configuration.GetValue<string>("Configuration:AppBasePath"));
if (Configuration.GetValue<bool>("Configuration:IsProduction"))
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
Console.WriteLine("prod");
}
else
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
Console.WriteLine("dév");
}
if (Configuration.GetValue<bool>("Configuration:IsProduction"))
app.UseBlazorFrameworkFiles("/PBM");
else
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToPage("/BlazorApp");
});
}
}
For the DLL files's locations, I thought there were 2 DLL groups : one for the Server project(located in 'app folder'/_framework and the second one for the client project(located in 'app folder'/wwwroot/_framework.
(see here at microsoft.com :
The client Blazor WebAssembly app is published into the /bin/Release/{TARGET FRAMEWORK}/publish/wwwroot folder of the server app, along with any other static web assets of the server app. The two apps are deployed together.
My problem being that the DLLs of the server project aren't in the right place.
But I can make a mistake.
Here is the folders content:
'app root folder':
I can see Blazor.Canvas.dll, which is a dll of the client.
'wwwroot'/_framework:
EDIT:
BlazorApp is an alternative to index.html, because I couldn't inject the IConfiguration instance in index.html, so I use BlazorApp.cshtml instead. It is located in the server project, in the Pages directory.
Its content:
#page "/"
#using Microsoft.Extensions.Configuration
#using Microsoft.Extensions.Hosting
#inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnv
#inject IConfiguration Config
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Application</title>
#if (Config.GetValue<bool>("Configuration:IsProduction"))
{
<h1>PROD</h1>
<base href="/PBM/" />
}
else
{
<h1>DEV</h1>
<base href="/" />
}
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<script src="_framework/blazor.webassembly.js"></script>
<script src="js/AnchorLink.js"></script>
<script src="js/focus.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.9.4/dist/Chart.min.js"></script>
<!-- This is the glue between Blazor and Chart.js -->
<script src="_content/ChartJs.Blazor.Fork/ChartJsBlazorInterop.js"></script>
<!-- composant AnchorNavigation-->
<script>
function whitifyDashboardMenuItems() {
document.getElementById("syntheseMenu").classList.remove("active");
document.getElementById("patientsMenu").classList.remove("active");
document.getElementById("preopMenu").classList.remove("active");
document.getElementById("transfusionMenu").classList.remove("active");
document.getElementById("postopMenu").classList.remove("active");
document.getElementById("suiviJ30Menu").classList.remove("active");
document.getElementById("dmsMenu").classList.remove("active");
return 0;
}
function BlazorScrollToId(id) {
const element = document.getElementById(id);
if (element instanceof HTMLElement) {
element.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest"
});
}
}
function BlazorScrollDirectToId(id) {
const element = document.getElementById(id);
if (element instanceof HTMLElement) {
element.scrollIntoView({
behavior: "auto",
block: "start",
inline: "nearest"
});
}
}
function changeMenuAfterStartup() {
const dms = document.getElementById("dmsMenu");
if (dms instanceof HTMLElement)
dms.classList.remove("active");
const synthese = document.getElementById("syntheseMenu");
if (synthese instanceof HTMLElement)
synthese.classList.add("active");
return 0;
}
function iFrameTestMethod() {
var iframe = document.getElementById('iFrameTest');
console.log(iframe);
//var content = iframe.contentDocument.body;
if (iframe.readyState == 'complete') {
console.log('hoho');
}
}
function checkScrollSpy() {
var scrollSpys = [].slice.call(document.querySelectorAll(".scrollspy-example"));
var scrollSpysLength = scrollSpys.length;
for (var i = scrollSpysLength; i--;) {
var $spy = $(scrollSpys[i]);
$.fn['scrollspy'].call($spy, $spy.data());
}
}
function resizeIFrameToFitContent(iFrame) {
alert("in resizeIFrameToFitContent");
iFrame.height = iFrame.contentWindow.document.body.scrollHeight;
alert(iFrame.height);
}
function resizeiFrames() {
alert("in resizeiFrames");
var iFrame = document.getElementById("iFrame1");
resizeIFrameToFitContent(iFrame);
// or, to resize all iframes:
var iframes = document.querySelectorAll("iframe");
for (var i = 0; i < iframes.length; i++) {
resizeIFrameToFitContent(iframes[i]);
}
return 0;
}
function autoResize(iframe) {
alert(iframe);
var h0 = iframe.contentWindow;
alert(h0);
var h = iframe.contentWindow.document.body.scrollHeight + "px";
alert("h:" + h);
iframe.style.height = h;
}
window.SetFocusToElement = (element) => {
element.focus();
return 0;
};
function getHeight(element) {
return element.offsetHeight;
}
caches.delete("blazor-resources-/").then(function (e) {
console.log("'blazor-resources-/' cache deleted");
});
function RefreshIFramesDashboard() {
var elts = document.getElementsByClassName("refresh");
for (var elt of elts) {
//elt.contentWindow.location.reload();
//elt.parentNode.replaceChild(elt.cloneNode(), elt);
}
}
function RefreshIFramesDashboard(newCohort) {
var elts = document.getElementsByClassName("refreshParent");
for (var elt of elts) {
//elt.contentWindow.location.reload();
//elt.parentNode.replaceChild(elt.cloneNode(), elt);
elt.innerHTML = elt.innerHTML.replace(/Cohort=./, "Cohort=" + newCohort);
}
}
</script>
</head>
<body data-spy="scroll" data-target="#list-example" data-offset="85" class="scrollspy-example" style="position:relative;overflow-y: scroll;scroll-behavior: smooth;">
<app>Loading...</app>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
<div id="blazor-error-ui">
An unhandled error has occurred.
Reload
<a class="dismiss">🗙</a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
thank you.
Try to add MIME Types to iis to slove this issue:
Open IIS Manager and click on your server level settings.
In Features View, double-click MIME Types.
In the Actions pane, click Add.
In the Extension box, type .dll. In the MIME Type box, type application/octet-stream.
Click OK.

No assembly found containing a Startup or [AssemblyName].Startup class

I've tried resolving this from answers in other and similar posts, but no luck.
I'm Using MVC 5, framework 4.8 latest VS2017.
Thanks
My Config is: (including other attempts)
<configuration>
<appSettings>
<!--<add key="owin:AutomaticAppStartup" value="false" />-->
<add key="owin:HandleAllRequests" value="true"/>
<!--<add key="owin:AppStartup" value="Api.xxx" />-->
</appSettings>
</configuration>
Startup class is:
[assembly: OwinStartupAttribute(typeof(Api.xxx.Startup))]
namespace Api.xxx
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Allow all origins
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
….
}
}
}
and Api is:
namespace Api.xxx
{
[Route("values")]
public class ValuesController : ApiController
{
private static readonly Random _random = new Random();
public IEnumerable<string> Get()
{
var random = new Random();
return new[]
{
_random.Next(0, 10).ToString(),
_random.Next(0, 10).ToString()
};
}
}
}
I think you need to change
[assembly: OwinStartupAttribute(typeof(Api.xxx.Startup))]
to
[assembly: OwinStartup(typeof(Api.xxx.Startup))]
Reference: https://learn.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection

404 Not Found error when running ServiceStack on IIS8 Express

Regarding to this thread: 404 Not found
I still have this issue on Win 8.1 - VS 2013-1
<!--<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>-->
<location path="api">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
and
public class HelloAppHost : AppHostBase
{
/// <summary>
/// Initializes a new instance of your ServiceStack application, with the specified name and assembly containing the services.
/// </summary>
public HelloAppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }
/// <summary>
/// Configure the container with the necessary routes for your ServiceStack application.
/// </summary>
/// <param name="container">The built-in IoC used with ServiceStack.</param>
public override void Configure(Container container)
{
//Register user-defined REST-ful urls. You can access the service at the url similar to the following.
//http://localhost/ServiceStack.Hello/servicestack/hello or http://localhost/ServiceStack.Hello/servicestack/hello/John%20Doe
//You can change /servicestack/ to a custom path in the web.config.
SetConfig(new HostConfig
{
HandlerFactoryPath = "api"
});
SetConfig(new HostConfig { DebugMode = true });
Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}");
}
}
When I uncomment the second system.webServer tag, I only get HandlerNotFound Exceptions from the api route. When I remove the location tag in web.config the same errors occur.
Like it is now it works ...
Any help for clarification appreciated,
thanks Norbert
You need to change the following:
SetConfig(new HostConfig
{
HandlerFactoryPath = "api"
});
SetConfig(new HostConfig { DebugMode = true });
to
SetConfig(new HostConfig
{
HandlerFactoryPath = "/api",
DebugMode = true
};
Just a guess, but your second instance of HostConfig is probably overriding the first one.

Correct way to enable autocomplete in SharePoint (Asp.Net) forms?

There is a problem in SharePoint (both WSS3 and WSS2) that item registration and edit forms do not have the "autocomplete" feature in Internet Explorer.
That is, if you need the same value in some text field frequently, you have to type it manually. Internet Explorer does not provide you with drop-down list of values you have previously entered. In FireFox this thing works, however.
As I found out from this response to a similar question, it is because Internet Explorer disables auto-complete in pages which have "no-cache" or "expires" headers. SharePoint really does send non-cacheable pages to the client. This SO response also says that one should add autocomplete="on" to the form tag, it overrides the cache headers.
I edited the FORM element in "default master" page on my server to always include the autocomplete="on" and - yes, autocomplete feature works!
However, Microsoft warns us NOT to edit the "default.master" as it will be overwritten by the next service pack or patch.
So, the question is - what are my options to correctly solve this situation? I want to have autocomplete to be enabled in whole server farm.
However, Microsoft warns us NOT to
edit the "default.master" as it will
be overwritten by the next service
pack or patch.
Copy & Paste new masterpage with different name and use it as the default one. Use either SharePoint designer or programmatically set SPWeb.MasterUrl and/or SPWeb.CustomMasterPage.
For this, i have 2 features
One to set custom master page on current website
Other one to activate previous feature on all webs + staple feature to newly created web's
Project http://img251.imageshack.us/img251/7351/ss20100312093605.png
(MWSBalticovo is for meeting workspace - they have a different masterpage)
Web scoped feature for a single web site to use custom masterpage.
I have a feature with my custom master page packaged:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="BalticovoMasterPages" List="116" Url="_catalogs/masterpage" RootWebOnly="TRUE" Path="MasterPages">
<File Url="Balticovo.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
<Property Name="ContentType" Value="$Resources:core,MasterPage;"/>
<Property Name="MasterPageDescription" Value="$Resources:Balticovo,BalticovoMasterPageDescription;"/>
</File>
<File Url="MWSBalticovo.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
<Property Name="ContentType" Value="$Resources:core,MasterPage;"/>
<Property Name="MasterPageDescription" Value="$Resources:Balticovo,MWSBalticovoMasterPageDescription;"/>
</File>
</Module>
</Elements>
And FeatureReceiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
string masterUrl = "/_catalogs/masterpage/Balticovo.master";
string mwsMasterUrl = "/_catalogs/masterpage/MWSBalticovo.master";
if (web.CustomMasterUrl.ToLower().Contains("/mws")) //meeting workspace
web.CustomMasterUrl = mwsMasterUrl;
else
web.CustomMasterUrl = masterUrl;
web.MasterUrl = masterUrl;
web.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.MasterUrl = "/_catalogs/masterpage/default.master";
if (web.CustomMasterUrl.ToLower().Contains("/mws")) //meetng workspace
web.CustomMasterUrl = "/_catalogs/masterpage/MWSdefault.master";
else
web.CustomMasterUrl = "/_catalogs/masterpage/default.master";
web.Update();
}
2nd Site scoped feature to enable previous features
elements.xml (activate 1st feature on newly created web's, but will not activate on existing):
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation
TemplateName="GLOBAL"
Id="{227c6aed-f66b-482d-aea8-a2af3ca203b7}" />
</Elements>
FeatureReceiver (activate 1st feature on existing webs):
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
Guid masterPageFeatureId = new Guid("{227c6aed-f66b-482d-aea8-a2af3ca203b7}");
SPSite site = properties.Feature.Parent as SPSite;
SPWebCollection webs = site.AllWebs;
foreach (SPWeb web in webs)
{
try
{
if (web.Features[masterPageFeatureId] == null)
web.Features.Add(masterPageFeatureId);
}
catch (InvalidOperationException) //target feature not yet installed
{ throw; }
catch (SPException) { } //If feature could not be activated.
finally
{
if (web != null)
web.Dispose();
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
Guid masterPageFeatureId = new Guid("{227c6aed-f66b-482d-aea8-a2af3ca203b7}");
SPSite site = properties.Feature.Parent as SPSite;
SPWebCollection webs = site.AllWebs;
foreach (SPWeb web in webs)
{
try
{
if (web.Features[masterPageFeatureId] == null)
web.Features.Remove(masterPageFeatureId);
}
catch (InvalidOperationException) { }
catch (SPException) { }
finally
{
if (web != null)
web.Dispose();
}
}
}

Resources