How to check if MVC bundle exists - asp.net-mvc

Is there a way to test if a bundle exists before trying to render it?
I want to do something like this:
#{
var bundleName = GetBundleName();
}
#if (Scripts.BundleExists(bundleName))
{
#Scripts.Render(bundleName)
}
Obviously, Scripts.BundleExists() isn't real, but it there something build in that does this? Or do I have to implement this myself?

You can get the bundles in the View by: var bl = System.Web.Optimization.BundleTable.Bundles; Then you can search the collection for a specific bundle by path as registered in BundleConfig. After that check if the path or any of the included paths exist.

I'm not aware of any way (nor was I able to find a way) for you to do this that is built into the framework. If you really have to do this, I would point you to a solution by Herman.
Asp.Net MVC Bundling, best way to detect missing file
Are your bundles dynamic? If not, I would suggest that this may not be something you need. Once you have them setup correctly the first time, they shouldn't fail.

Related

How to apply bundle transform in debug mode?

In my project, I want to send application settings to the browser from the server.
To do so, I have created a class named "ConfigFileTransform", that inherits from IBundleTransform. In the process method, I replace keywords in javascript by their values. (Maybe it is not the best solution...)
For example, the query limit for a type of object is set to the client using this transform class.
My problem comes when I debug my application, I see the debugger going to my custom bundle transform class, but the rendered javascript does not contain the replacements...
In release mode, everything is ok.
Does anyone know what I can do to see my transforms applied when I am in debug mode?
Put this in the Application_Start method in your Global.asax file.
BundleTable.EnableOptimizations = true;
I haven't worked with only applying certain transforms but taking a look at this post:
ASP.Net MVC Bundles and Minification
You should be able to do this. You might need to refactor your bundle code a little so that you can add Conditional Compilation Variables to clear your transforms in debug only. So it could look something like this:
var noMinify = new ScriptBundle("~/bundles/toNotMinify").Include(
"~/Scripts/xxxxxx.js"
);
#if DEBUG
noMinify.Transforms.Clear();
noMinify.Transforms.Add(new ConfigFileTransform())
#endif
_bundles.Add(noMinify);

Class Loading in Phalcon for plugins

When adding a dispacher in the services.php it doesnt seem to have access to the autoloader to include class's.
Example: /config/services.php
$di->set('dispatcher', function() use ($di) {
require __DIR__.'/../../app/plugins/security.php';
$eventsManager = $di->getShared('eventsManager');
$security = new Security($di);
$eventsManager->attach('dispatch', $security);
$dispatcher = new Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
Is it correct to include the require? Its the only way I can seem to have access to the security plugin? Im sure there is a better way?
Both https://github.com/phalcon/invo is different to the demonstration on the Phalcon homepage?
Anyone clarify?
If you plan move your plugins directory to another directory there is a high possibility of breaking the application due to the static paths. An autoloader gives you the freedom to re-organize the application by just adjusting the paths on it.
The INVO application uses an autoloader:
Set a plugins directory:
https://github.com/phalcon/invo/blob/master/public/index.php#L20
Use the class with auto-loading:
https://github.com/phalcon/invo/blob/master/public/index.php#L38
I think public/index.php is a better place for require or in the same place you can use a loader

Using ASP.NET Javascript Bundles from the controller

I realise this breaks the MVC pattern, but there is a viable reason for doing it this way in an application I am currently building :)
What I am trying to do is output a JavaScript bundle directly from the Controller rather than via a link via a View.
So for example I have a bundle called "~/jQueryPlugin" what I'd like to do is something along the lines of
return this.JavaScript(BundleTable.GetBundle("~jQueryPlugin").BundleContent)"
However for the life of me I cannot figure out what the BundleTable.GetBundle("~jQueryPlugin").BundleContent part should be in order to get a string representation of the combined minimized bundle.
Any help would be appreciatedĀ·
In the 1.1-alpha1 release we added a new Optimizer class which should allow you to more easily do this. Its intended to be a standalone class that's useable out of side of ASP.NET hosting, so setting it up will be slightly different.
You can get the bundle contents out via something like this:
OptimizationSettings config = new OptimizationSettings() {
ApplicationPath = "<your physical path to the app>",
BundleSetupMethod = (bundles) => {
bundles.Add(new ScriptBundle("~/bundles/js").Include("~/scripts/jqueryPlugin.js"));
}
};
BundleResponse response = Optimizer.BuildBundle("~/bundles/js", config);
Assert.IsNotNull(response);
Assert.AreEqual("<your bundle js contents>", response.Content);
Assert.AreEqual(JsMinify.JsContentType, response.ContentType);
The next release should be fleshing this scenario out more, as it is needed for build time bundling integration with Visual Studio.

Lint for ASP.NET MVC?

Is there a lint utility for ASP.NET MVC? Given that I frequently specify views and links via strings, when I move things around or change entity names I often break things, which I then only find out about when something fails at runtime.
ReSharper's v6 (whose nightlies are now available, if you don't mind living on the edge) will catch this kind of error for you.
You can use Refactor -> Rename and enable Search in Strings to replace every string in the solution
Other option -- use the strongly typed helpers (which might still be in the futures assemblies). EG, Html.Action<ProductsController>(x => x.ShowProduct(id)) ; really the only way to fly.
I don't know that there's something like that, but I'll tell you what I do: All my view names are in a struct that contains string constants. It's a pain to keep it sync'ed as the project changes, but it's worth it because you're far more likely to catch errors if you're using
ViewNames.Customer
rather than
"customer"

Extract A Solution File From WSS

I'm trying to figure out how to extract a solution file .wsp file from a SharePoint server. Is this possible and if so how?
You can make a small console application and access the solutions using the SPFarm.Local.Solutions property. Include the Microsoft.SharePoint.Administration namespace and use the following code snippet to download the solution file:
SPSolutionCollection solutions = SPFarm.Local.Solutions;
foreach (SPSolution solution in solutions)
{
SPPersistedFile wspFile = solution.SolutionFile;
wspFile.SaveAs("c:\\Temp\\Solutions\\" + solution.Name);
}
You need to make sure that your output directory exists before calling the SaveAs() method. If it does not exists an exception is thrown.
You can access the Solutions on a farm by using the SPFarm.Local.Solutions property. I'm not sure if you can retrieve the underlying file though. That's where I'd start.

Resources