ASP.NET MVC locating cs files generated from cshtml? - asp.net-mvc

So I've recently started with ASP.NET MVC 4. I'm using the razor engine.
My question is concerning the view files, with suffix cshtml. It seems to me that these are precompiled into *.cs files by razor which in turn are then compiled into MSIL. (A pattern that is familiar from my days as a JSP developer.) One reason why I am making this assumption is that if I enter some invalid c# code into the cshtml file I get a compilation error displayed like this:
Line 34: public class _Page_Views_BaseDataAdmin_Index_cshtml : ...
And line 34 is not indicative of where the error is in the cshtml file, just as the class _Page_Views_BaseDataAdmin_Index_cshtml seems to refer to a regular .net class not the view file.
So my question is: Where do I find the cs file? Specifically, in the example above, "_Page_Views_BaseDataAdmin_Index_cshtml.cs"? Maybe I need to add some config to tell MVC to keep this .cs file on disk, if so, how do I do this?
Thanks!

A quick tip to find the generated files is to add this to your .cshtml file.
This will show you immediately the full path
<div>This file is compiled to #this.GetType().Assembly.CodeBase</div>

You can find the compiled views in your Temporary ASP.NET Files folder.
First find the .compiled file that corresponds to your view (ex: home.cshtml):
home.cshtml.a8d08dba.compiled
This file contains the assembly name: assembly="App_Web_hkc53urb"
Your .cs file will be the assembly name concatenated with a number: App_Web_hkc53urb.1.cs
An easier approach might be to use Windows search for your view name in the temp ASP.NET directory.

Related

Blazor Server localization

I have a project using Localization. program.cs:
builder.Services.AddLocalization();
...
app.UseRequestLocalization(new RequestLocalizationOptions()
.SetDefaultCulture(LanguageList.GetSupportedLanguages[0])
.AddSupportedCultures(LanguageList.GetSupportedLanguages)
.AddSupportedUICultures(LanguageList.GetSupportedLanguages));
Where LanguageList.GetSupportedLanguages is nothing but an array of CultureInfo.
It's working perfectly; however, I have put the .resx files in the same folder as the razor files and the folder content gets quite messy fast. If I move them, it doesn't work anymore.
I read in Microsoft doc that the Resource file path can be set by:
builder.Services.AddLocalization(config => config.ResourcesPath = "Resources");
However, if I move the resx files to this folder two things happen. It doesn't work (text is not set) and I get this error message:
Custom tool PublicResXFileCodeGenerator failed to produce an output for input file 'Resources\Index.en.resx' but did not log a specific error.
I been trying some tips I found here on StackOverflow, among them adjusting the folder structure to mimic the namespace.
No luck. What am I doing wrong here? How do I utilize the ResourcesPath setting?

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

Properties.Resources.XXXX Namespace or Module not Defined

I'm trying to read in the text (as a string) of an XML file from my Resources. The XML file is named MyXMLResourceFile.resx.
I tried using the C# way using
let myFile : string = Properties.Resources.MyXMLResourceFile
but it is giving me the error (under Properties):
The namespace or module 'Properties' is not defined.
I'm assuming I'm missing an open but have no idea what it would be.
Sorry, I'm still pretty new to F# coming from VB.
Additional information:
I made the resx file myself per this SO answer I then looked to see how others accessed it. In C# they did Properties.Resources.XXXX per this project I saw you could use ResourceManager but didn't see any method to read the whole file directly into a string.
The namespace or module 'Properties' is not defined.
That's because the namespace Properties is not defined. You probably brought the resx file from another project. Open the associated .Designer file, if it doesn't appear in the F# project, open it up in the original project. You will see a namespace with and a bunch of C# code, assuming the original project was in C#. The namespace of this file has to be the same namespace of the project where you are trying to call it.
But, since this is C# code, it won't run on a F# project. You have two choices: use a code generator to generate the associated .Designer class in F# code with the Properties namespace, (I don't know if such generator exists); or pre-compile the resx in the original project and reference it as a dll from your F# project, then you can access it with ResourceManager.

Subsonic MVC Template and Code Generation

I am using Subsonic MVC Template from this project.
Here is a folder named [Code Template] which contains the .tt files for generating views and controllers. But that is giving me error
"MvcTextTemplateHost not found"
I want to generate the create/edit/list pages based on the tables generated by subsonic. How can I do that?
what you want is this:
http://github.com/subsonic/SubSonic-3.0/downloads
1 add subsonic dll to bin folder
2 add reference to that DLL
3 add a connection string to your web.config or app.config
add the connection string name to your ttinclude file
drag files into your models folder
you are then ready to go
http://subsonicproject.com/docs/The_5_Minute_Demo
brilliant up and running in like 10 mins

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