Changing file references during the Publishing of the website - asp.net-mvc

I have a master file which has references to the JS and the CSS file used for my website. These references are pointing to the non-crunched version of the files. Now, when I hit the Publish button through Visual Studio for my project I want to change the references in my master file to point to the crunched version of the JS and CSS.
Eg: During development if it was pointing to http://www.example.com/main.js, during publish it should change the reference to http://www.example.com/main_min.js. Is there a way to do this?
Also, before changing the reference I need to run my current js file(main.js) through the tool which outputs the crunched js file(main_min.js).
Any help on this is appreciated!
Thanks.

You could go to the project's properties and in the Build Events tab enter the following into the "Post-build event command line":
$(SolutionDir)..\YourJsCruncher.exe $(ProjectDir)\content\js\debug\ $(ProjectDir)\content\js\release\
and then have a custom HTML helper:
public static MvcHtmlString IncludeJs(this UrlHelper helper, string javascriptFile)
{
#if DEBUG
var subfolder = "debug";
#else
var subfolder = "release";
#endif
var path = helper.Content("~/Content/js/{1}/{2}.js", subfolder, javascriptFile);
return MvcHtmlString.Create(string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", path));
}
and then in your view:
<%= Url.IncludeJs("foo.js") %>

This question has most of the answers you need:
Concatenate and minify JavaScript on the fly OR at build time - ASP.NET MVC
Customizing the publich process for one click publish is a little trickier. The best I could answer is linking to this blog: http://vishaljoshi.blogspot.com/ When I was working with one-click-publish customization this is the place I found the most answers. If I remember correctly this wasn't trivial and its better to integrate this into your build process like the answers in the first question I link explain.

Related

How can I reference a script that is in my pages directory?

I'm trying to keep a good practice of keeping context together and in that regard, I like how Razor Pages forces you to place your view models next to its view. I'd like to do this with script files as well.
For example, if script is a bit large and is only used on this particular page, it may not be the cleanest thing to have it in a render section in the cshtml. Instead, I'd want to place it into its own .js file. But to keep the project easy to navigate for team members, I'd like to place that .js file in the same folder next to its view and view model. I'm not sure how to link that file, or if I even can. If I give src a path to the file, it is not found. Assuming that's because wwwroot is the only folder setup to host static files.
Suggestions?
You can add an option to the StaticFilesProvider in your Configure method:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),"Pages")), RequestPath="/Pages"
});
See the documentation on Static Files in ASP.NET Core: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.1

how to load html files within the application in javafx

i am using jdk1.7.0_45 for building javafx project,loads pre created html site from my application,found passing this url file:///E:/web/index.html
but which is not my requirement,need to load from my app,so please tell me anyone,the folder name need to place in javafx app.
thanks
I'm assuming that the question is that you want to package a static HTML file with your application and display it in a WebView.
You can do it with code like this (Scala syntax but the meaning should be clear):
val url = getClass getResource "index.html"
webView.getEngine load url.toString
In this example, the index.html file (and any related content) must reside in the same package as the class of the code. E.g. if the class is mypackage.myapp.MyApp, the file must be in the mypackage.myapp package.
If your build system provides an e.g. resources folder, it would be good practice to use that for static content such as this. I.e., for this example, create a package mypackage.myapp in the resources folder and place the html file there.

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.

How do I publish/deploy an MVC Project with ResX Resources?

I have written a VERY simple MVC application which just displays a single string from a Resource file. It works just fine on my local machine but when I deploy the project to the web server I get the error.
CS0103: The name 'Resources' does not
exist in the current context
You can very easily replicate exactly what I am doing in just 10 steps!
Create a New MVC 2 Web Application.
(File->New->Project->ASP.NET MVC 2 Web Application, say no to the Unit Testing Project)
Add the "App_GlobalResources" folder.(right click the project and select Add->Add ASP.NET Folder->App_GlobalResources)
Add a Resource File to this folder.(right click the folder and select Add->New Item...->Resources File. Name it Strings.resx)
Add a single string to the Resource table.(Name = "HelloWorld", Value = "I localized Hello World!")
Set the File Properties for the Resource File.(Click the file Strings.resx and int the Properties window set Build Action = "Embedded Resource" and the CustomTool = "PublicResXFileCodeGenerator")
Add a new Controller(Right click the Controllers folder and select Add->Controller... Name it HelloWorldController.cs)
Add a the View(With the cursor in the Index method of the HelloWorldController.cs Press CTRL-M-V. Use the default values including View name = "Index")
Modify the View so that it displays our string from the resource file.Replace the content of the MainContent placeholder with
<h2><%: Resources.Strings.HelloWorld %></h2>
Run it locally to test that it works. Which it should.
Publish it to a web server and visit the url "http://localhost/HelloWorld"
This is where I see the error described at the top.
I would imagine that the settings I've put on the ResX file are incorrect and the resource is not published to the server.
Help is greatly appreciated.
Thanks!
Ah ha! Figured it out. In LARGE part to this article:
http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx
Sounds like the App_GlobalResources folder is NOT cooperative with MVC. So I moved my ResX file to a new folder~/Resources/Strings/Strings.resx
This along with 1 minor change to set the File Property
Custom Tool Namespace = Resources
and Problem Solved!

ASP.NET MVC IIS problem

I have this piece of code in a .cs file in an ASP.NET MVC application:
HtmlTableCell r2c1 = new HtmlTableCell();
r2.Cells.Add(r2c1);
r2c1.ColSpan = 2;
r2c1.Style.Add("font", "1px arial");
r2c1.Style.Add("height", "10px");
r2c1.Style.Add("background-image", "url(/Content/Images/pagebgbottomwhite.jpg)");
r2c1.Style.Add("background-repeat", "repeat-x");
This works OK locally, but when I deploy my app using IIS 5 I don't see that picture.
How can I change that format of the URL so I can see it?
First off, you don't really want to have this kind of code in your presenter.
As for URL format, try Server.MapPath("~/Content/Images/pagebgbottomwhite.jpg");. And ensure that this file is indeed where it should be.
You really ought to be using CSS and defining a class that has these attributes. The url would then be relative to the location of the CSS file in the site: url(../Images/pagebgbottomwhite.jpg) -- assuming that your css file is in a sibling directory to Images. Then you would apply the CSS class to your element.
I also agree with Anton that, using MVC, this code should not be in your controllers/models, but rather in the view -- in which case you would not be using HtmlTableCell. In that case, and using pure CSS, it's simply a matter of creating the proper row in the table.
<tr><td class="bottom-row" colspan="2"></td></tr>
Confirm that this file (/Content/Images/pagebgbottomwhite.jpg) is deployed. Is it set not to copy or was it left behind in deployment.

Resources