|
Maybe just getting the current Directory and adding wwwroot to it is the way to go for seeding.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
The code in Startup runs as part of the BuildWebHost method in the Program class. That gets invoked both by the application, and by the design-time EF commands (add-migration , update-database , etc.), so you can't seed the data in the Startup class.
In 2.0 projects using EF Core 2.0, Program.BuildWebHost is invoked to obtain the application services. Unlike 1.x, this has the additional side effect of invoking Startup.Configure . If your 1.x app invoked database initialization code in its Configure method, unexpected problems can occur.
So the seeding code needs to be in Main , on in the context's OnModelCreating method[^].
In theory, you should be able to inject any registered service into your DbContext constructor, which would let you access your ServerPath service from the OnModelCreating method.
Alternatively, you can retrieve the services from the Main method using services.GetRequiredService<ServerPath>(); within the try block.
(Personally, I tend to keep the migrations and seed data in a separate console app, because I don't want the website connecting to SQL using an account that has permissions to create and modify databases.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hmm....
I'll leave the seed in Main for now, and make sure everything works before reconsidering a change.
But I changed the file paths from Url types to physical types with Path.Combine and I successfully seeded my database with the blobs and Url paths and wrote the images to the folders. Getting past System.Drawing yesterday was whole nother issue.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Last question of the day I hope.
I've been job hunting over the last 3 months. In my area there are all these Senior MVC Full Stack Developer job postings in which they ask for the following qualifications. I suspect it's all these different recruiting companies looking for one person in general, or someone considering leaving Silicon Valley and heading south.
MVC 5,
.Net Core
c#, VB
CSS, CSS3
HTML5
Javascript, JQuery
SQL Server, Stored Transactions
ORM, CRUD, Azure, Git, NodeJS
AngularJS 2-5
What I can't figure out is why the AngularJS with MVC. AngularJS seems to be a client side version of MVC. From what I have read, a crawler can't read an AngularJS page created 100% by AngularJS, so you have to use something that will download a static page first like MVC, then you can inject some AngularJS to say continue showing more cars.
I would think that ReactJS would be more suitable over AngularJS, being able to use the same server side controller and reducing code writing. Then say if the price of the car changes, the ReactJS polling will update that without a page refresh.
Last but not least, how the heck do these guys have the time to master all of these skills. Do these programmers really exist?
I didn't pickup Angular because I was busy learning Bootstrap 3 and 4, and writing responsive CSS.
I'm just trying to understand the posting, and would love to visit the job requester to see how they are using this and learn.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
You're querying the combination of angular and MVC, but not c# and vb.net?
You get all sorts of rubbish in jobs ads, they're just throwing lots of tech in to cast their net wide and get as many applicants as possible, hoping to maybe get someone who knows every technology out there in the process. You won't really need all of those things though. It's up to you to decide if you want to spend time dealing with people who post adverts like that, or if you want to deal with people who are a bit more focused in what they want.
|
|
|
|
|
Good Reply. Guess non programmers are composing these job postings and just throwing out rubbish.
Perhaps I'm suppose to call them on it, or play the game.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
jkirkerx wrote: AngularJS 2-5 To clarify, the team announced that separate terms should be used for each framework with "AngularJS" referring to the 1.X versions and "Angular" without the "JS" referring to versions 2 and up.
I'm amazed they're not asking for "8+ years experience with ASP.NET Core", given it was released back in 2016.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Didn't know that.
I had one recruiter call me and ask me questions about Angular. In the end I asked here why and she wasn't able to answer the question. Then I said your not a programmer are you and she replied no. They must copy and paste these job requirements and claim them as their own.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Well I have two functions called Startup and I just need one I think. One was stock called Startup(IConfiguration configuration) that gets called from Program.cs I think, and the other I added from examples in how to read the appsettings.json file called Startup(IHostingEnviroment env) and how to initialize the database.
So basically being so new here, I must of confused versions such as core 1 vs core 2
I would assume the first is correct.
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
And the 2nd one is incorrect for core 2.1
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
using (var client = new IndigoDBContext())
{
client.Database.EnsureCreated();
client.Database.Migrate();
}
Configuration = builder.Build();
}
But I don't see another place that has the environment except for Configure. Maybe the 2nd code goes there.
I'm trying to get my database created, and set migration.
Question is: I suspect I'm on the wrong track here, and I need a little help getting squared up on this. I probably have duplicates of or version overlaps here.
This is my ConfigureServices Function, I'm pretty sure I have this right
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
<pre>
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddApplicationPart(typeof(ErrorController).GetTypeInfo().Assembly);
services.AddHttpContextAccessor();
services.Configure<AppConfiguration>(Configuration.GetSection("AppConfiguration"));
services.Configure<SmtpConfig>(Configuration.GetSection("SmtpConnection"));
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddEntityFrameworkSqlite().AddDbContext<indigodbcontext>();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddHttpContextAccessor();
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
You're right - the IHostingEnvironment overload is not needed in core 2. The web host builder in Program.cs will initialize the configuration system for you:
Loads app configuration from:
- appsettings.json
- appsettings.{Environment}.json
- User secrets when the app runs in the Development environment using the entry assembly.
- Environment variables.
- Command-line arguments.
My DbContext configuration is slightly different, but I'm using full SQL Server:
services.AddDbContext<CmsDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); Configuring a DbContext - EF Core | Microsoft Docs[^]
You don't want to call EnsureCreated if you're using migrations - it doesn't use the migrations. Database.Migrate() will create the database if it doesn't exist.
asp.net - How and where to call Database.EnsureCreated and Database.Migrate? - Stack Overflow[^]
I notice you've got two services.AddHttpContextAccessor(); lines. You only need one of them.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I got rid of the first one and kept the 2nd Startup(IHostingEnviroment env)
And I just deleted the 2nd services.AddHttpContextAccessor()
Then I noticed that my database was created, but in the root of the project solution and not in the wwwroot in which I created an App_Data folder. Not sure where to really put the Sqlite file.
I tried a similar DBContext to your, but it generated an error.
This works, well is created the database, not sure if it really works
services.AddEntityFrameworkSqlite().AddDbContext<IndigoDBContext>();
but when I use yours, I get a blank page
services.AddDbContext<IndigoDBContext>(options => options.UseSqlite(Configuration.GetConnectionString("SqLiteConnection")));
Maybe it has something to do with my DBContext OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "jkirkerx.sqlite;" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
Well that's a different subject.
I'm satisfied with my Startup.cs at the moment now
Thanks Richard!
This is hard!, it's a totally different way of thinking. But I think I see the purpose of it. I predict that EG. I won't have to create a new DBContext in Linq everytime, and will just be able to use the one created in startup.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
When I wrote my first Angular app in MVC, I added a RenderSection in the Body element so I could add the AngularJS component.
This is in my _homeLayout View.
<body @RenderSection("AngularJS", required: false)>
But in Core 2.1, it complains about C# being there. I read some articles about tag helpers and looked at some work arounds but I can't think of a way or method to replace this with.
I knew this would come back and haunt me in the future. Just looking for a better idea.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
A tag helper would be a fairly simple way to do this:
[HtmlTargetElement(Attributes = ViewBagKeyAttributeName)]
public class HtmlElementAttributeTagHelper : TagHelper
{
private const string ViewBagKeyAttributeName = "asp-attributes-key";
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
[HtmlAttributeName(ViewBagKeyAttributeName)]
public string AttributesKey { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (!string.IsNullOrWhiteSpace(AttributesKey))
{
var attributes = ViewContext.ViewData[AttributesKey];
switch (attributes)
{
case IDictionary<string, object> dict:
{
foreach (var attr in dict)
{
output.Attributes.Add(attr.Key, attr.Value);
}
break;
}
case object obj:
{
foreach (var attr in HtmlHelper.AnonymousObjectToHtmlAttributes(obj))
{
output.Attributes.Add(attr.Key, attr.Value);
}
break;
}
}
}
}
} In Views\_ViewImports.cshtml , add a line to register the tag helper:
...
@addTagHelper *, YourProjectAssemblyName In Views\Shared\Layout.cshtml , add the tag helper to the body:
<body asp-attributes-key="BodyAttributes"> In your controller or view, store the body attributes in the ViewData (or ViewBag , which is basically the same thing):
ViewData["BodyAttributes"] = new Dictionary<string, object>
{
["ng-app"] = "",
...
};
ViewBag.BodyAttributes = new
{
ng_app = "",
...
}; Any attributes you add to the ViewData collection will be combined with any existing attributes on the body element.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I read up on Tag Helpers, and it looked so complicated compared to the old way.
So what folder would I place that in to stay organized?
Like ViewComponents?
FYI:
I'm slowly getting my stuff from the 2nd project wired up in the first project.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I tend to put mine in a folder called "Helpers", but there's no specific requirement. Just so long as you're consistent, you can put them wherever you want.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I decided to take up .Net Core 2.1 and port over a project to it, which now seems like a bad idea.
I've noticed that some of the support in system.web has been deleted such as
HttpContext to get querystrings, Request.Url, Session Value, Request.Headers for User Agent
HttpCookies
And reading the Web.Config file that doesn't exist anymore.
My original project design had a separate project that contained extra controllers, controller Attributes, security Attributes
and used the above to operate. it also contained the Entity Framework context, models, seeds, etc and needs a connection; and contains all the email stuff as well which requires access to appSettings.json.
I haven't been able to run the new app yet, for I'm refactoring the external .net core 2.1 project.
My question is, will I end up learning that I can't load dependency injections from the primary project to the secondary project?
Won't be able to load connection strings, and I'll have to move parts back to the primary project?
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Maybe take advice from your own signature
|
|
|
|
|
No Joke because it's not broken.
This is for job training. Here in my area, Employers are looking for .Net MVC people that know MVC 5, Core 2 and AngularJS 2 through 5., NodeJS and more. I'm just trying to keep up with the bar since it's been raised again.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
jkirkerx wrote: My question is, will I end up learning that I can't load dependency injections from the primary project to the secondary project? Simple answer: no.
If anything, DI is easier in ASP.NET Core than in previous versions, because it's baked-in. Just declare the dependencies in your constructor, and register your services in the Startup.ConfigureServices method, and the framework will wire everything together for you.
(If you don't want to explicitly register every single implementation of a service, you can use something like Scrutor[^] to find and register all implementations in an assembly.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard.
I didn't want to find out at the end that it could not be done and have to start again.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Hi,
Iam converting Html page to pdf using html2canvas.js and jspdf.debug.js but images are not exporting to pdf.Can anyone help me to overcome this issue.
Thanks in Advance.
|
|
|
|
|
I'm scratching my head on this one.
Decided to learn ReactJS and did the tutorial on Tutorial (ASP.NET Core) | ReactJS.NET on ReactJS.Net. I did the client side, and then implemented the server side, but I can't get the server side to render the HTML from the ReactJS file.
In ReactConfig, I loaded all the scripts here, but I really only need the server version of the script.
ReactSiteConfiguration.Configuration = new ReactSiteConfiguration()
.AddScript("~/js/remarkable/dist/remarkable.min.js")
.AddScript("~/ReactJS/HelloWorld.jsx")
.AddScript("~/ReactJS/ReactJS_Client.jsx")
.AddScript("~/ReactJS/ReactJS_Server.jsx");
I loaded my scripts in my Section, that loads at the bottom of the page.
This may sound confusing, but I loaded the client version of the script here. I did this because the server side script is different.
<script src="~/js/bundle_ReactJS_Production.min.js"></script>
<script src="@Url.Content("~/ReactJS/ReactJS_Client.jsx")"></script>
@Html.ReactInitJavaScript()
So down later in the View, I have my little ReactJS section, in which the documentation says I need to use this object for server side rendering.
But it crashes with a blank page on the IIS server, and I can't figure out why. I take it out, and the page loads fine but executes the client version instead. I checked the page source and the ReactJS HTML wasn't there.
All I can think of is that I'm missing something in the deployment, but what?
Or I need to add something to the web server.
<div class="container">
<div class="media-container-row">
<div class="col-12 col-md-10" id="content">
<!-- This will render the component server-side -->
@Html.React("CommentBox", new
{
initialData = Model,
url = Url.Action("/Home/Comments"),
submitUrl = Url.Action("/Home/AddComment"),
pollInterval = 15000
})
</div>
</div>
</div>
If it ain't broke don't fix it
Discover my world at jkirkerx.com
modified 21-Jun-18 13:57pm.
|
|
|
|
|
I finally figured it out.
Diving into ReactJS.net is confusing, until you get a complete understanding of how it works on the server side.
So the ReactJS.Net package seems to be something that was working a couple of years ago, but has been upgraded with new packages or DLL's that are more refined. So it's one thing to do the tutorial to get the client side working, and a whole different story to get the server side working.
What it boiled down to, as far as it not working on my server was the Chakra Engine or simply server side rendering. I'm running server core with no browser, and the Javascript rendering engine needs Internet Explorer or Edge to render server side (That's why it worked on my desktop). If you don't have it loaded, then you have to build your project with an JavacriptEngineBuilder which comes in many flavors, and create a JavascriptEngineSwitcher configuration.
Last but not least, is the Remarkable. This is what ran me through the hoops for hours. Loading ReactJS files n ReactSiteConfiguration is not required, you just need remarkable and your jsx scripts.
This deems an article to be written on the subject.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Hi,
Am working on Converstion of html to pdf using Html2pdf.bundle.js but am getting Blurry Pdf Output.Can anyone suggest me to overcome this error.
Thanks in advance.
|
|
|
|
|
I had the same issue when working with that library. I ultimately opted to render my pdfs on the server side; I didn't find a good resolution.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|