How can I read version of project in dotnet core (formerly asp.net mvc6) - asp.net-core

how can I read assembly version information for my project, the value which in this case comes from the from project.json, and read that in my ASP.net core Controller and pass it to the view?

You can use Razor to get this right from the View and bypass the Controller.
<b>Version</b> #(
Assembly.GetAssembly(typeof(HomeController))
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion
)

To get your application version, as it exists in project.json, you would write:
string appVersion = Assembly.
GetEntryAssembly().
GetCustomAttribute<AssemblyInformationalVersionAttribute>().
InformationalVersion;
Additionally, if you want to know which version of .net core your app is running on you can do this:
....
string dotNetRuntimeVersion = typeof(RuntimeEnvironment)
.GetTypeInfo()
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
You may need to add these units to your using for the above runtime version snippet:
using Microsoft.DotNet.InternalAbstractions;
using System.Reflection;

You can get the AssemblyInformationalVersionAttribute from your project's assembly and then pass it to the view.
Here's how to get that attribute: https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs#L35-L44

In an early beta version you could add constructor to your controller with parameter IApplicationEnvironment. this param have a property with Version name
public HomeController(IApplicationEnvironment appEnvironment) {
this.appEnvironment = appEnvironment;
}
(No longer works in ASP.net core 1.0)

Related

DiffGrams for .NET Core. We are upgrading our project to .NET Core 3.x so need to find the DiffGrams used in the previous version of .NET

We are upgrading our .NET 2.0 application to .NET Core 3.x there's a DiffGrams used to capture the Table field updates (before/after values) used for Auditing purpose. I have to achieve the similar in the .NET Core 3.x. I am not sure which one is the equivalent for .NET Core 3.x.
Could you anyone help me guide on this? Thank you.
DataSet.WriteXml/DataSet.ReadXml method applies to .NET Core 3.x.
The WriteXml method provides a way to write either data only, or both data and schema from a DataSet into an XML document.
private void WriteXmlToFile(DataSet thisDataSet)
{
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "XmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream stream = new System.IO.FileStream
(filename, System.IO.FileMode.Create);
// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter xmlWriter =
new System.Xml.XmlTextWriter(stream,
System.Text.Encoding.Unicode);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(xmlWriter, XmlWriteMode.DiffGram);
xmlWriter.Close();
}
The resultant XML code is rooted in the <diffgr:diffgram> node and contains up to three distinct data sections, as follows:
<diffgr:diffgram>
<MyDataSet>
:
</MyDataSet>
<diffgr:before>
:
</diffgr:before>
<diffgr:errors>
:
</diffgr:errors>
</diffgr:diffgram>

JsonApiDotNetCore 4.0 has removed BuildResourceGraph from JsonApiOptions. What is the replacement?

I am migrating from .NET Core 2.2 to 3.1. In doing so, I have updated the JsonApiDotNetCore package from 3.1 to 4.0.0 alpha 4.
In 2.2, I used JsonApiDotNetCore 3.1 and was using BuildResourceGraph to add any JSON API resources to the resource graph. Code below:
IMvcCoreBuilder objMvcCoreBuilder = null;
objServices.AddJsonApi((objOptions) =>
{
objOptions.BuildResourceGraph((objBuilder) =>
{
objBuilder
.AddResource<Register>("registers")
.AddResource<Client>("clients")
;
});
}, objMvcCoreBuilder);
But, I get the following error:
'JsonApiOptions' does not contain a definition for
'BuildResourceGraph' and no accessible extension method
'BuildResourceGraph' accepting a first argument of type
'JsonApiOptions' could be found (are you missing a using directive or
an assembly reference?)
What is the replacement for BuildResourceGraph?
After digging through the JsonApiOptions.cs commit history on Git, I found the change:
IMvcCoreBuilder objMvcCoreBuilder = null;
objServices.AddJsonApi(
options => options.Namespace = "api/v1",
resources: resources =>
resources
.AddResource<Register>("registers")
.AddResource<Client>("clients")
,
mvcBuilder: objMvcCoreBuilder
);
Go down to the /NoEntityFrameworkExample/Startup.cs file and you will see the diff that shows the change. Other than that, there is only a cryptic mention to renaming BuildResourceManager in the change log notes at the top of the site.
https://github.com/json-api-dotnet/JsonApiDotNetCore/commit/7b8250bf5b9e64b91d8fa0357e915a1121eb6081#diff-d56ca61ff20d8be0b7cb20c9fd106d9f
The revised version of the file is here:
https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/7b8250bf5b9e64b91d8fa0357e915a1121eb6081/src/Examples/NoEntityFrameworkExample/Startup.cs

How to send parameters to Stimulsoft Report in Asp.Net Core 2.0?

I use Asp.net Core 2.0 Web Application.
I have a simple stimulsoft report file.
my report has a parameter Called #ID.
If I Use the report without parameter, it's working well, But When I send parameter to report, it's not working and report loaded empty.
my Code is:
var _Report = new Stimulsoft.Report.StiReport();
_Report.Load(#"C:\temp\report.mrt"); // load report
((StiSqlDatabase)_Report.Dictionary.Databases["Connection"]).ConnectionString = "new connectionString "; // change connection
_Report.DataSources["DataSource1"].Parameters["#ID"].ParameterValue = 5171; // set parameter value
_Report.Render();
Note: I use Asp.net Core version 2.0.
this code is working in asp.net well.
please help me.
thanks.
Wow!!!
solved.
I should use from Value Instead ParameterValue.
Like below:
_Report.DataSources["DataSource1"].Parameters["#ID"].Value = 5171;

cannot apply indexing with [] to an expression of type System.Web.Routing.RouteValueDictionary

I have in unit test code:
var result = (RedirectToRouteResult)controller.Create();
Assert.AreEqual("AddUnits", result.RouteValues["action"]);
But result.RouteValues["action"] gives me error:
cannot apply indexing with [] to an expression of type System.Web.Routing.RouteValueDictionary
I use ASP.NET MVC 4
Although RedirectToRouteResult is defined in System.Web.Mvc assembly, RouteValueDictionary is defined in System.Web assembly (since it's part of the ASP.Net Routing mechansim that was introduced prior to the MVC framework).
Try to add a reference to System.Web in your test project and see if that helps.
Add reference to System.Web.Routing
You may be using MVC 5.
If that is the case (which was my case), here is a solution, from Unit Testing Controller Actions in MVC 5 (part 4 - Using RouteValues dictionary):
When trying to assert values returned within an action result we
utilise the ActionResults RouteValueDictionar, e.g.
Assert.AreEqual(“Index”, result.RouteValues[“action”],“Unexpected View Name”);
However, doing so in your MVC 5 unit test project may throw
the following compile error: Cannot apply indexing with [] to an expression of type ‘System.Web.Routing.RouteValueDictionary’
Solution: Add System.Web.Routing 4.0 and System.Web 4.0 from the “Add
Reference” dialog. Routing is dependent on system.web. Add the
System.Web.Routing using declaration to your class file.
Your code should look like
var result = (RedirectToRouteResult)controller.Create();
Assert.AreEqual("AddUnits", ((RouteValueDictionary)result.RouteValues)["action"]);
Pay attention to the cast.
Add the reference of System.Web to your test project. It help in my case.
You just need to add the reference of "System.Web" and "System.Web.Routing" in your test Project. You can simply add it by right click on reference in test project.

Using Forms after migrating from playframework 2.0 to 2.1 RC2 (java)

I have updated my controllers with to use play.data.Form.form() method instead of Controller.form(). When I try to run my application I get errors like:
error: method render in class create_user cannot be applied to given types;
return ok(create_user.render("", Form.form(CreateUserInfo.class), creator.get()));
required: String, play.api.data.Form, User
found: String, play.data.Form, User
It looks like the my templates expect to get play.api.data.Form instead of play.data.Form. Is there suppost to be any implicit conversion or should I update my templates to use play.data.Form?
If I'm using play.data.Form in my templates I am missing out on some of the features of play.api.data.Form, like the ability to request parameters through the apply method ( ex: createUserForm("username") )
The solution was provided by Guillaume Bort at the playframework google group.
I forgot to add javaCore as a dependency for my application after updating Build.scala. You have to explicitly add javaCore as a dependency in 2.1.
val appDependencies = Seq(
javaCore
)
And remember to start using play.Project instead of PlayProject:
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)