I'm trying to make an ASP.NET Core 2.0 application multi-language. I created a resource file under a folder "Resources" called "Resource.it.resx". I set its access modifier to Public and its Namespace to Resources.
After (re)building the solution I cannot see this namespace in the C# code now in the cshtml code.
Is there some other steps to do?
There are two things you need to take into account:
Only a Resource.resx generates a namespace, culture-specific resources such as Resource.it.resx does not generate a namespace. This is the intended behavior.
ASP.NET Core's localization practices suggest not to use resources directly, but rather find the localized strings using IStringLocalizer.
I suggest you to read the fundamentals of ASP.NET Core localization in the official MSDN guide. There you will find examples for localizing strings in Controllers, Views and wherever you need them.
First thing check if your resource namespace is visible in controller,
like Resources.Resource.
I had not and in this situation I have created emtpy resource class, ex. my resources name is ServiceResources.en-US.resx, in same folder I have ServiceResources.cs empty class too.
Check if you have imported your namespace in _ViewImports.cshtml correctly, with IStringLocalizer class.
Related
I am building a class library in C# with .NET 6 (possibly 7) for use with ASP.NET Core sites.
I would like this class library to (among other things), contain a configuration UI (kinda like how Swashbuckle builds up an OpenAPI UI - I checked out the source code and couldn't quite wrap my mind around that portion).
I figured creating controllers/views in the class library would be the way to go.
The controllers are working automatically (although, I'm mildly concerned about routing conflicts -- what if I have a /foo/bar route in my class library and the project using this library also has a /foo/bar route?).
The views, however, do not seem to be added automatically. I've tried embedding and following the folder conventions, but I still get the error:
An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Index' was not found. The
following locations were searched: /Views/Foo/Index.cshtml
/Views/Shared/Index.cshtml
Here's the relevant portion of the class library:
What do I have to do to get these Views loaded/parsed/working from a class library? (Or is there a better alternative to doing what I'm trying to do without Views?)
ApplicationParts is the answer, but I found the documentation a little hard to follow, so here's what I did:
var assembly = typeof(FooController).Assembly;
#this.AddControllersWithViews()
.AddApplicationPart(assembly)
.AddRazorRuntimeCompilation();
#this.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{ options.FileProviders.Add(new EmbeddedFileProvider(assembly)); });
The key for me was .AddRazorRuntimeCompilation(); and the following line to add a file provider.
I'm trying to understand how to do an Asp.Net Core application (3.0) looks like a Screaming Architecture folders and files conventions.
I read about it but and started with an empty project. The folders are:
Controllers
Views
Models
I want to make the web application working like
Customers/Controllers
Customers/Views
Customers/Models
Is it possible?
Thanks in advance.
Both controllers and models are referenced by namespace, so their physical file locations have no bearing on anything. Controllers are dynamically loaded regardless of where they are in the project or even if they're in the project at all (i.e. a controller from a referenced library will automatically be loaded). Models are entirely code-based, and you'd just add a using statement with the namespace of the model(s) to access them in any other piece of code.
Views, however, are very much based on the filesystem, and changing the main folder they're in from just Views or Pages directly in the project root will cause the convention-based view loading to fail completely. You can always add additional view search locations, such that it will look in /Foo/Views and /Bar/Views, etc. but that's really not recommended.
All that said, though, there is a concept of areas, and you can break down your controllers, models, and views that way. You'd simply have to have:
Areas/Customers/Controllers
Areas/Customers/Views
Areas/Customers/Models
In other words, the Areas prefix would be mandatory.
The only documentation I can find regarding localization in .NET Core only covers the case with one .resx file per class/view. Is there no way to have a single .resx with all string resources in it? At least one per project.
For example, I'm using AddDataAnnotationsLocalization to get localization of validation errors. If I have a ViewModel A that inherits some base properties from another ViewModel B, I will need two resx files A.resx and B.resx where both of them will have the strings from B defined.
It would help me a lot if it was possible to just put all strings in a single file.
I have a class in my Class Library that's doing all sorts of validations and consistency checks for files before returning the result to the Web, and it used to work fine in WebForms with:
System.Web.HttpContext.Current.Server.MapPath("myFilePath here");
But now that I'm doing the same with MVC, the routing is messing up the MapPath.
How can I get the "base" path of the application in the Class Library using MvC?
Use:
HttpContext.Server.MapPath("~/myFilePath here");
I normally end up passing that path from controller to the helper library where it is needed.
Other option is using:
System.Web.Hosting.HostingEnvironment.MapPath("~/Your relative path from root website")
BTW, to use HostingEnvironment.MapPath(), you will need to add reference to System.Web.
Is there a way to import a dll from a class project (or maybe another web application project) into a web application project and reuse a #helper? The reusability for web applications with Razor seems pretty much zero if that cannot be achieved.
You can do it, but you have to jump through several hoops.
You need to obtain RazorGenerator: "This is a Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution. "
Using RazorGenerator, you can create .cshtml files in your class library project that declare helper functions. For example, in a file called Foo.cshtml:
#helper MyHelper(string parameter) {<text>#parameter</text>}
These helpers will exist as static methods in the static class representing your .cshtml file. In the above example, that would translate to Foo.MyHelper.
You can invoke these static methods from your web application's .cshtml file just like any other static method. (assuming you've added the correct using directives to point to the namespace containing your helper.) For example:
<div>#Foo.MyHelper("hello world")</div>