MVC2 To MVC3 Html.Raw(Json.Encode - json

I have a project developed in MVC2 / ASPX / c#. I updated it to MVC and it works well. Just noticed when I publish it, get propblem with System.Web.Helpers; and copy it manually.
Since the application is very complex I develop additional compenents in new projects. I used MVC3 ASPX views, in my view I use:
var series = <%= Html.Raw(Json.Encode(ViewBag.Series)) %>;
it works great, when I integrate the controller, view and master page in the older application I get this error:
The name 'Json' does not exist in the current context
Would appreciate your suggestions.

The problem was in System.Web.Helpers, I was using version 2.0; it should be version 1.0.

Related

How to use the ABAP namespace /ABC/ in the custom SAPUI5 application?

I am new to the SAPUi5 things. I am developing a custom Fiori application using using SAP Web IDE. I Manually deploy the application to the system using report /UI5/UI5_REPOSITORY_LOAD.
Initially I used the project name in the Web IDE with name 'ZAPP' and gave the same name while deploying the app to ABAP system. Everything worked fine.
Now, I have a requirement where I need to use the namespace in the project name e.g. '/ABC/APP'. How do I achieve this?
PS: I have tried deploying the ZAPP and replaced all the occurrences of 'ZAPP' with '/ABC/APP', but it gives me a following error:
Uncaught Error: The provided argument '/ABC/APP' may not start with a slash
at toUrl (ui5loader-dbg.js:1994:10)
at r.toUrl (ui5loader-dbg.js:1979:11)
at e._applyManifest (ComponentMetadata-dbg.js:173:13)
at e.getManifestObject (ComponentMetadata-dbg.js:315:5)
at e.init (ComponentMetadata-dbg.js:192:4)
at p._initCompositeSupport (Component-dbg.js:626:4)
at ManagedObject-dbg.js:534:11
at f.constructor (ManagedObject-dbg.js:558:4)
at f.constructor (Component-dbg.js:293:17)
at f.constructor (UIComponent-dbg.js:81:14)
Any help in this regard would be really appreated :-)

Generate Razor HTML emails in dotnet core 2

How can you generate emails (html) using Razor in dotnetcore - and not from an MVC app (think from a console app)?
RazorEngine does a great job in .net 4.x, but is not working in dotnet core.
RazorEngineLight works in dotnet core 1.x, but not in 2.x.
Some other options are mentioned in this post: Using Razor outside of MVC in .NET Core but none of them actually work in .net core 2.0
Edit two years later:
In case somebody comes here looking for answers on this... I (OP) have stopped entirely relying on Razor to generate emails using templates etc. It is very fragile and error-prone - a non-stop headache. I prefer Mandrill or Sendgrid these days - using templates.
In a comment on this provided answer from the link provided you stated
I am not able to get this to work. I get the error: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Razor.IRazorViewEngine' while attempting to activate 'Mvc.RenderViewToString.RazorViewToStringRenderer'.'
This normally indicates that a required service was not registered with the service collection so the provider is unable to resolve the service when needed.
That answer did not refer to the additional service configuration and only had
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IViewRender, ViewRender>();
}
as it was already being run in an Asp.Net Core environment, which meant that the services manually added in the console application were already being done in start up.
Pay attention to this snippet from the answer that was linked to from the answer you commented on.
private static void ConfigureDefaultServices(IServiceCollection services) {
var applicationEnvironment = PlatformServices.Default.Application;
services.AddSingleton(applicationEnvironment);
var appDirectory = Directory.GetCurrentDirectory();
var environment = new HostingEnvironment
{
WebRootFileProvider = new PhysicalFileProvider(appDirectory),
ApplicationName = "RenderRazorToString"
};
services.AddSingleton<IHostingEnvironment>(environment);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
});
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddLogging();
services.AddMvc();
services.AddSingleton<RazorViewToStringRenderer>();
}
The important part above is
services.AddMvc();
That will add the relevant view engine dependencies to the service collection
MvcServiceCollectionExtensions.cs
public static IMvcBuilder AddMvc(this IServiceCollection services) {
//...code removed for brevity
// Default framework order
builder.AddFormatterMappings();
builder.AddViews();
builder.AddRazorViewEngine();
builder.AddRazorPages();
builder.AddCacheTagHelper();
//...code removed for brevity
}
Everything else as currently presented is sound and should work as intended.
You should review
https://github.com/aspnet/Entropy/tree/93ee2cf54eb700c4bf8ad3251f627c8f1a07fb17/samples/Mvc.RenderViewToString
and follow a similar structure to get the code to work in your scenario. From there you can start making your custom modification and monitor where it breaks.
The modular nature of .Net Core allows for such customizations as the different modules can be stripped out and used in other environments.

How to distribute razor views to another application in .NET Core

I have created web application - Asp.Net MVC in .NET Core.
This application contains some Razor Views but I would like to share these views to another application like for example with DLL or like middleware.
Here is some information about example with distribution Controllers but around Views nothing special - https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/app-parts
I've tried add Controller like this:
var assembly = typeof(Project.HomeController).GetTypeInfo().Assembly;
services.AddMvc()
.AddApplicationPart(assembly);
This works very well, but I don't know how add the Views.
How can I distribute the Razor Views to another application? Is it way import them like a middleware to the MVC middleware?
You can create a normal netstandard1.6 library-i.e., where your controllers are, and embed the view resources into that dll in your csproj using the following:
<ItemGroup>
<EmbeddedResource Include="Views\**\*.cshtml" />
</ItemGroup>
After that, you can then register these using the RazorViewEngineOptions:
// Add views provided in this assembly.
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Add(
new EmbeddedFileProvider(typeof(ClassInLibrary).GetTypeInfo().Assembly));
});
Where "ClassInLibrary" is a class in your library that you can then get the assembly information from.

How do I render a view to a string in dotnet core

I would like to build emails in dotnet core 1.0 using the Razor engine, but I cannot work out how to do that.
I have created a view model containing the data for the view and I have made a view and run that view in code:
var confirm = new ConfirmEmailHtmlViewModel();
confirm.CallbackUrl = callbackUrl;
var message = View("ConfirmEmailHtml", confirm);
What I cannot figure out is how to extract a string containing the rendered view from 'message'.
Is this possible and if it is how do I do that?
I have answered this here: Where are the ControllerContext and ViewEngines properties in MVC 6 Controller?
The original answer on that question was for a pre-release of ASP.NET Core and not the 1.0 release.

Yii2 access to actions in new controllers

Comrades, I have had issues implementing yii2 basic but I'm yet to give up. I have successfully installed yii2, activated pretty url and created the .htaccess file in the root folder. The Home, About, Contact and Login urlswork fine.
i. I have created a new model, InstTypes with the CRUD. Why does http://localhost:8081/we#ss/instTypes/create return Not Found (#404)?
ii. I have also created a module instClients. I can access the index action in the DefaultControler. I have a model Insts with its CRUD under this modules. Why does http://localhost:8081/we#ss/instClients/insts/create return Not Found (#404)?
I tend to think that this could be due to the removal of the import and autoload from the config.
Could someone demonstrate how they've created a new model and CRUD and successfully accessed its actions?
Thanks in advance
I have created a new model, InstTypes with the CRUD. Why does
http://localhost:8081/we#ss/instTypes/create return Not Found (#404)?
You cannot access a model directly, the only way to interact with a model is via a controller which will intern interact with the model and the view. By initializing the model $model = new YourModelNmae(); or by rendering a view.
From your URL
http://localhost:8081/we#ss/instTypes/create
InstType should be your controller while create is an action under InstType
Using Yii2 Gii tool to generate a CRUD do the following : -
Generate your model
Generate your CRUD
go to
http://localhost:8081/we#ss/yourController/YourActionOnYourController
Refer to this youtube link for more detail https://www.youtube.com/watch?v=6B52-li6IgU
Happy coding :)
Just to add to the other answers, your url isn't working because it's camelCase. You need to make it hyphenated, so
http://localhost:8081/we#ss/instTypes/create
will become
http://localhost:8081/we#ss/inst-types/create
You're getting the 404 because you're trying to access instTypes, it should be inst-types.
this solved my problem. I needed to add the controllers to the controller map.