How to use HtmlTextWritter in MVC6 - c#

I am a beginner in mvc. When I upgrade my project into MVC6, I have face some problems and also unable to find the solution. My code block is :
public static void WriteTo(HtmlHelper helper, string value)
{
var baseWriter = helper.ViewContext.Writer;
using (HtmlTextWriter textWriter = new HtmlTextWriter(baseWriter))
{
textWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
textWriter.RenderBeginTag(HtmlTextWriterTag.Script);
textWriter.Write(value);
textWriter.RenderEndTag();
}
}
If there is any way to achieve the same scenario(like use of the HtmlTextWritter) in MVC6 or if there is any mistake in my code block.can you please suggest the solution. Even tried with google, but I can't get the opt solution.

The MVC equivalent of that archaic format (HtmlTextWriter is for WebForms) is this:
<script type="text/javascript">#value</script>
Where value is a variable that is present in your view.

Related

C#: solving Non-linear system using Microsoft solver

Using Microsoft Solver Foundation, I have only solved linear problem so far. I'm now trying to solve a very simple non-linear problem but for some reasons, Microsoft solver cannot solve it.
The problem is maximising a0*a1 , with a0<10 and a1<20 .
Here is the code I'm using:
using System;
using Microsoft.SolverFoundation.Services;
namespace SolverFoundationDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nBegin Solver demo\n");
var solver = SolverContext.GetContext();
var model = solver.CreateModel();
var decision1 = new Decision(Domain.RealNonnegative, "a0"); model.AddDecision(decision1);
var decision2 = new Decision(Domain.RealNonnegative, "a1"); model.AddDecision(decision2);
model.AddConstraint("Constraint0", "a0 <=10");
model.AddConstraint("Constraint1", "a1 <=20");
model.AddGoal("Goal", GoalKind.Maximize, " a0*a1 ");
var solution = solver.Solve();
Console.WriteLine("\nEnd Solver demo\n");
Console.ReadLine();
}
}
}
The error I get is " The model is not convex" , which is true but I was expecting Microsoft solver to be smart enough to find a solution anyway.
Thanks a lot for your feedback.
Regards,
Actually, Microsoft Solver choose NLP by default. For some reasons, if I add a useless constraint like a0*a1 < 1000000, it works. If I don't add this constraint, it does not work..
I'm really not satisfied with this Solver. Erwin, could you please tell me which solver should I use If I want to switch from Microsoft solver?
My program is written is C#. I have all my decisions and constraints inside Datable, and I pass it to the solver like shown below. In order to avoid to re-write the whole stuff, I would like a solver that I can feed using the same method..
string Comment = Convert.ToString(Table_Constraints.Rows[i]["Comment"]);
string Constraints = Convert.ToString(Table_Constraints.Rows[i]["Constraint"]);
model.AddConstraint(Comment, Constraints);

RazorLight with .Net Core 2.0 WebApi System.TypeLoadException

I have followed the following link to use a Razor style syntax with my email templates;
Microsoft Code Razorlight Tutorial
I have implemented a static helper class for getting my templates.
public static string GetTraderApplicationDeletedTemplate(Trader trader)
{
string templatePath = $#"{Directory.GetCurrentDirectory()}\EmailTemplates";
IRazorLightEngine engine = EngineFactory.CreatePhysical(templatePath);
string result = engine.Parse("TraderDeletedEmail.cshtml", trader);
return result;
}
However, when debugging the engine.Parse method is throwing a System.TypeLoadException with no further detail?
Now the templatePath is valid (as is the file name .cshtml)? Does anyone have any ideas on what could be causing this error?

Read build configurations programmatically

I want to be able to read the VS build configurations programmatically. That is because I want to create my own builder.
How do I do that? Does anyone have code example?
What i mean is that if I have Debug, Development, Release I want them to be listed in a list box in a Form application. I have tried using the "EnvDTE.dll" class but I am not sure it is what I am looking for. If anyone has a concrete example or link to an example I would be more than grateful.
You can use the msbuild API. In Visual Studio 2015, there is a class called Microsoft.Build.Construction.SolutionFile in the Microsoft.Build.dll that ships with VS2015 that you can use to load a solution.
In VS2013 there is no such thing, but you can do the following:
(reference Microsoft.Build.dll, Microsoft.Build.Engine.dll, Microsoft.Build.Framework.dll)
class Program
{
static void Main(string[] args)
{
string nameOfSolutionForThisProject = #"MySolutionFile.sln";
string wrapperContent = SolutionWrapperProject.Generate(nameOfSolutionForThisProject, toolsVersionOverride: null, projectBuildEventContext: null);
byte[] rawWrapperContent = Encoding.Unicode.GetBytes(wrapperContent.ToCharArray());
using (MemoryStream memStream = new MemoryStream(rawWrapperContent))
using (XmlTextReader xmlReader = new XmlTextReader(memStream))
{
Project proj = ProjectCollection.GlobalProjectCollection.LoadProject(xmlReader);
foreach (var p in proj.ConditionedProperties)
{
Console.WriteLine(p.Key);
Console.WriteLine(string.Join(", ", p.Value));
}
}
Console.ReadLine();
}
}
ConditionedProperties contains a list of platforms and configurations contained in the solution. You can use this to populate your forms.

How to access database in lightswitch in other class?

I just don't know how to explain this clearly. So I create a simple image pattern of what I did.
My question is, how would I be able to access my database in other class in LS?
I've been searching on net, but I didn't found any solution. I hope I'll find it here.
Thanks!.
Any suggestion is already appreciated.
Thanks for the answer Bryan, but I found the answer on my problem here Richard Waddell
Here is what I did to achieve my goal.
Switch your LS project to file view
Go to "Common" project, under "UserCode" folder, create a class (e.g. Authenticate.cs) and put this codes.
The code follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
public class Authenticate
{
public static adminuser GetCurrentUser()
{
adminuser userFound = (from useritem in
Application.Current.CreateDataWorkspace().basecampcoreData.adminusers
where useritem.LoginID == Application.Current.User.Name
select useritem).SingleOrDefault();
if (userFound != null)
return userFound;
else
return null;
}
}
}
Then you can now call the Authenticate.GetCurrentUser() anywhere in the project.
Thanks!
The main difference is the first set of code that works is running inside a screen. For your Authenticate class, you need to do the following steps to access the Database.
Note: I'm assuming that your datasource has the default name of ApplicationData since you hid the name, if not, make the corresponding changes. If it's a completely different datasource, change "_IntrinsicData" in the steps below)
These steps are taken from the Lightswitch Help Website
Navigate to ..ServerGenerated\GeneratedArtifacts (in the LightSwitch project) and click on ApplicationData.cs and Add As Link.
Add the following code below, this code dynamically creates a connection to the database. LightSwitch uses “_IntrinsicData” as it’s connection string.
private ApplicationDataObjectContext m_context;
public ApplicationDataObjectContext Context
{
get
{
if (this.m_context == null)
{
string connString =
System.Web.Configuration.WebConfigurationManager
.ConnectionStrings["_IntrinsicData"].ConnectionString;
EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
builder.Metadata =
"res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
builder.Provider =
"System.Data.SqlClient";
builder.ProviderConnectionString = connString;
this.m_context = new ApplicationDataObjectContext(builder.ConnectionString);
}
return this.m_context;
}
}
You should be able to access it through Context.adminusers

Compiling an ASPX page into a standalone program

As I mentioned here, I'm trying to generate HTML from an ASPX page inside a WinForms.
I'm trying to compile the ASPX page directly into the EXE; I'd like to be able to write something like this:
var page = new ASP.MyPageName();
var stringWriter = new StringWriter();
using(var htmlWriter = new HtmlTextWriter(stringWriter))
page.RenderControl(htmlWriter);
I added an ASPX page, set the Build Action to Compile, and put in the following Page declaration:
<%# Page Language="C#" ClassName="MyPageName" %>
The code compiles, and the properties that I defined in the ASPX are usable from the calling code, but the StringWriter remains empty. I tried calling htmlWriter.Flush, and it didn't help.
The page instance's Controls collection is empty, and it probably shouldn't be.
I looked at the EXE in Reflector and I couldn't find the page content anywhere. I therefore assume that the page isn't being compiled properly.
What is the correct way to do this?
I ended up using ApplicationHost.CreateApplicationHost to run the entire application in the ASP.Net AppDomain. This is far simpler and more reliable than my attempt to fake the ASP.Net AppDomain.
Note: In order to do this, you must put a copy of your EXE file (or whatever assembly contains the type passed to CreateApplicationHost) in your ASP.Net folder's Bin directory. This can be done in a post-build step. You can then handle AssemblyResolve to locate other assemblies in the original directory.
Alternatively, you can place the program itself and all DLLs in the ASP.Net's Bin directory.
NOTE: WinForms' Settings feature will not work in an ASP.Net AppDomain.
I believe what you want to use is the SimpleWorkerRequest.
Unfortunately, however, it requires that the resource (I believe) live on disk. From your description it sounds like you prefered for the whole app to reside in your DLL. If that is the case you will most likely need to implement your own HttpWorkerRequest.
Warning
This does not work reliably, and I've given up on it.
I ended up copying the files to the output folder and initializing ASP.Net in same AppDomain, using the following code: (I tested it; it sometimes works)
static class PageBuilder {
public static readonly string PageDirectory = Path.Combine(Path.GetDirectoryName(typeof(PageBuilder).Assembly.Location), "EmailPages");
static bool inited;
public static void InitDomain() {
if (inited) return;
var domain = AppDomain.CurrentDomain;
domain.SetData(".appDomain", "*");
domain.SetData(".appPath", PageDirectory);
domain.SetData(".appVPath", "/");
domain.SetData(".domainId", "MyProduct Domain");
domain.SetData(".appId", "MyProduct App");
domain.SetData(".hostingVirtualPath", "/");
var hostEnv = new HostingEnvironment();//The ctor registers the instance
//Ordinarily, the following method is called from app manager right after app domain (and hosting env) is created
//Since CreateAppDomainWithHostingEnvironment is never called here, I need to call Initialize myself.
//Here is the signaature of the method.
//internal void Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) {
var cmp = Activator.CreateInstance(typeof(HttpRuntime).Assembly.GetType("System.Web.Hosting.SimpleConfigMapPathFactory"));
typeof(HostingEnvironment).GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(hostEnv, new[] { ApplicationManager.GetApplicationManager(), null, cmp, null });
//This must be done after initializing the HostingEnvironment or it will initialize the config system.
SetDefaultCompilerVersion("v3.5");
inited = true;
}
static void SetDefaultCompilerVersion(string version) {
var info = CodeDomProvider.GetCompilerInfo("c#");
var options = (IDictionary<string, string>)typeof(CompilerInfo).GetProperty("ProviderOptions", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(info, null);
options["CompilerVersion"] = version;
}
public static TPage CreatePage<TPage>(string virtualPath) where TPage : Page {
return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(TPage)) as TPage;
}
//In a base class that inherits Page:
internal string RenderPage() {
var request = new SimpleWorkerRequest("", null, null);
ProcessRequest(new HttpContext(request));
using (var writer = new StringWriter(CultureInfo.InvariantCulture)) {
using (var htmlWriter = new HtmlTextWriter(writer))
RenderControl(htmlWriter);
return writer.ToString();
}
}
InitDomain must be called right when the program starts; otherwise, it throws an exception about the configuration system being already initialized.
Without the call to ProcessRequest, the page's Controls collection is empty.
UPDATE: The page is rendered during the call to ProcessRequest, so that must be done after manipulating the Page instance.
This code will not work if the program has a .config file; I made a method to set the default C# compiler version without a .config file using reflection.
Why dont you just look at hosting the ASP.NET runtime in your app?
There are several snippets online to show you how.
Here is one.
Most likely you are using wrong page class. You need to use not the actual nice-named class in code behind. During compilation ASP.NET generates page class, which inherits from class defined in code behind and within this class happens initialization of all the controls. Therefore you should use generated class (check its name using Reflector).
If you're looking for the MVC version of this answer, see:
Is there a way to process an MVC view (aspx file) from a non-web application?
The code uses a separate AppDomain, but as far as I could tell, this is required as all the code generated from an ASPX file depends on HttpContext and HostingEnvironment.VirtualPathProvider.
public class AspHost : MarshalByRefObject
{
public string _VirtualDir;
public string _PhysicalDir;
public string AspxToString(string aspx)
{
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter tw = new HtmlTextWriter(sw))
{
var workerRequest = new SimpleWorkerRequest(aspx, "", tw);
HttpContext.Current = new HttpContext(workerRequest);
object view = BuildManager.CreateInstanceFromVirtualPath(aspx, typeof(object));
Page viewPage = view as Page;
if (viewPage == null)
{
UserControl viewUserControl = view as UserControl;
if (viewUserControl != null)
{
viewPage = new Page();
viewPage.Controls.Add(viewUserControl);
}
}
if (viewPage != null)
{
HttpContext.Current.Server.Execute(viewPage, tw, true);
return sb.ToString();
}
throw new InvalidOperationException();
}
}
}
public static AspHost SetupFakeHttpContext(string physicalDir, string virtualDir)
{
return (AspHost)ApplicationHost.CreateApplicationHost(
typeof(AspHost), virtualDir, physicalDir);
}
}
Then, to render a file:
var host = AspHost.SetupFakeHttpContext("Path/To/Your/AspNetApplication", "/");
String rendered = host.AspxToString("~/Views/MyView.aspx");
you can use the ClienBuildManager class to compile ASPX files.

Categories

Resources