.status-msg-wrap { display: none; }
Sunday, March 31, 2019
no image

Confused about deployment

Hi all,

Another newbie question for you! I have finally wrapped my head around asp.net core mvc and I have made a few apps. I would now like to deploy them so I can link to them on a portfolio website.

I have been working on two options:

  1. Azure App Service (free tier) with a PostgreSQL DB hosted on my DO droplet. Is this possible? I currently have my app working locally and accessing my DO postgres db. However, when I publish the app to azure I get errors around reaching the db. Is this a routing issue? Or am I not correctly preparing my application to be published?
  2. Everything on a DO droplet. I am somewhat knowledgeable about RHEL/CentOS (I have the RHCSA at least). I played around with dokku which seems promising but I wanted to get a POC up on a vanilla CentOS droplet. I followed the docs and set it up with apache as a reverse proxy. I first added it to my droplet that had SSL through LetsEncrypt. After running into issues, I spun up another droplet and commented out the https stuff in my app to test. I am still getting a 503 no matter what I do. I can curl my app from the droplet at localhost:5000. Does that mean it is definitely the proxy or permissions on the app?

Sorry for all the crazy ranting. I am finally picking up speed with my development and I feel like I wasted my entire Sunday trying to deploy the damn things. I just want to get back to coding! I would be eternally grateful for any pointers.

Thanks!

Confused about deployment Click here
no image

Language specification in URL

There are many different methods for handling different languages and cultures on an web page. Among those have a subdomain dedicated to the language like https://en.mysite.com, using a route to specify it like https://mysite.com/en or leaving it out of the URL and having the option be stored in a cookie and default to some language. There are probably many more methods. Which method do you prefer and have the best experience with?

Language specification in URL Click here
no image

Using policies to route users to different controller methods?

Is this possible? I already have policy based auth set up but instead of doing an if chain or a switch on the role, I'd rather redirect users of role x to GetX() and role y to GetY(). Can't seem to find the answer by googling so any insights appreciated.

Using policies to route users to different controller methods? Click here
no image

Connecting to Azure Functions?

If I have a Client (browser/javascript) should it:
a) connect directly to Azure Functions?
b) connect to backend API (.Net core) which in turn connects to Functions?

What are pros/cons to each approach?

Connecting to Azure Functions? Click here
Saturday, March 30, 2019
no image

EFCore, services, viewmodels, interfaces... lots of confusion

Ok. I'm migrating to EFCore from our very outdated ways of linq to sql with sqlmetal and manual dbml modifications.

I'm reading that EFCore represents a repository and as such doesn't need interfaces for your db objects. So instead of passing around interfaces and automapping them to db objects and saving them I can just pass around the object itself and save it. Cool, except in viewmodels typically you decorate members with your form requirements for validation.

How do you do this without inheriting an interface in the vm and decorating the vm? Is automapper and a new class that you manually write the only way to do this?

If you decorate the database class itself that works but you'll proliferate your requirements throughout your web but sometimes I don't want to do that. In one viewmodel I might want to force a user to input a value but not in another, even though they use the same database object.

Am I doing this way wrong? Do I still need interfaces? I have read that EF doesn't work well with them, but I want to make sure my validation requirements are in the viewmodel so I don't have to manually set "data-val="true"" and all that in the client and then manually validate again on the server instead of using modelstate.

Sorry if these are stupid questions, I swear I've tried to research this as much as possible but it seems that every heavily upvoted response on SO is conflicting with each other and it just doesn't seem like there's a standard that most follow.

It would be awesome if there were a project out there on GH or wherever that I could use as a reference, the "golden project" where they do everything correctly and by the book.

I know some rules are subjective to preference and there are 10 million ways to do any one thing (which is good) but it's a bit overwhelming and I don't want to dig myself into a hole on a big project by setting up the foundation of said project incorrectly.

EFCore, services, viewmodels, interfaces... lots of confusion Click here
no image

One class per file for related EF classes?

I've been really taking advantage of the "Paste JSON as class" feature in visual studio for DTOs and entities. However, with a nested object, you often end up with something like this:

public class Rootobject

{

public string date { get; set; }

public bool active { get; set; }

public Start start { get; set; }

public End end { get; set; }

public Slot[] slots { get; set; }

}

public class Start

{

public string hour { get; set; }

public string minute { get; set; }

}

public class End

{

public string hour { get; set; }

public string minute { get; set; }

}

public class Slot

{

public string time { get; set; }

public string hour { get; set; }

public string minute { get; set; }

public bool blocked { get; set; }

}

Would you guys separate these out into their own files? Or just leave it in a RootObject.cs. I realize its just a style and practice thing, and I might just be being lazy, but I also don't want to litter my entities folder with stuff like the End object. I also do realize the Start and End objects are the same, but they need to be differentiated during reflection - just so I don't get roasted for that.

One class per file for related EF classes? Click here
Friday, March 29, 2019
no image

[DotNet MVC 5] Question: how to decouple the identification of specific data from both code and DB structure -- how to tell code when data is special without hard-coding the data’s primary keys into the code or relying on special database structure to annotate each row.

Got a bit of a problem here, and one of my difficulties is that I just don’t know the right terms to search for. Also, late at night here, so I apologize for any rambling and/or conceptual confusion. Not drunk -- just tired.

Let’s use a drop-down select menu as an example. We have six entries, drawn from a lookup table, and four of those entries need to hide or show different content on the page itself, such as additional or fewer form fields. Two don’t do anything explicit to the rest of the page content (as an example of null cases).

I would like to know what you use to “flag” those four entries such that the in-page code knows what to do when any one of those four items are selected in the drop-down menu.

The actions themselves are irrelevant, but could be as simple as expanding the form to include additional fields to fill out or select, or hiding parts of the form that are no longer relevant. The key thing is, however, that each of the four do something unique and different.

Up until now, I have used either one of two different methods.

One method is to add additional boolean columns to the lookup table that ends up populating the drop-down select menu. These additional columns would be specially and specifically named, and if these columns are ever exposed to the end-user admin in any sort of a CRUD functionality, the business logic of the site would ensure that for any one column, only one row had it checked off, and for any one row, only one column could be checked off (to ensure vertical and horizontal uniqueness).

The one benefit to these columns is that the code in the web page is somewhat easy to program for. Since we are bringing in the lookup table as a list to populate the drop-down select menu, we are also able to write out the array (including the boolean columns) into an inline piece of JQuery that can pair up the ID (which could be anything) with a boolean value for a specific column name. Once the drop-down select menu has focused on that specific ID, the JQuery would then know what to do because that ID has the boolean value of TRUE for a specific column that is associated with a specific chuck of JQuery actions.

The drawback is that any sort of a well-populated lookup table would then have many, many boolean columns. And adding a value to that lookup table (when it has added associated on-page functionality) would require both a code change and a database change (to add a new column). This is also brittle in that the code needs to explicitly know the column names (the boolean values) to pair up primary keys with actions.

My other method is a little simpler: adding the actual IDs of the lookup table items (the relevant ones with additional actions, that is) to a Resources file. This lets the code look at the resources file, bring that into the same JQuery script as above, and do the same thing.

This has the additional benefit of needing only a code change; there is no change to the structure of the database, and so it is simpler. However, in this case the actual ID of the lookup table items that change page functionality are being added directly to a resource file - this makes it brittle. If an item gets deleted out of the lookup table and re-created, there will be no way for an end-user admin to update the resource file on their own, unlike an admin backend with the appropriate CRUD interface that can re-associate that entry with its needed action (the boolean column in the first example).

I am hoping that someone out there might know of a third path that fully decouples the recognition of the data from either the code or the database structure, such that a more flexible, less brittle option can be constructed.

[DotNet MVC 5] Question: how to decouple the identification of specific data from both code and DB structure -- how to tell code when data is special without hard-coding the data’s primary keys into the code or relying on special database structure to annotate each row. Click here
no image

Using Dropzone to upload file to Azure Storage

Hi there!

I am trying to upload a file to azure storage after generating an SAS for an Azure Storage Container. I am using Dropzone.js to do so. I am getting a 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature

It seems as though no authorization header is being sent in my request. I am not sure how to fix that.

My code for the upload looks like this:

<form class="dropzone" id="file-upload" action="@Model.ContainerURI"></form> @section Scripts { <script type="text/javascript"> Dropzone.options.fileUpload = { //autoProcessQueue: false, //To prevent automatic upload before getting SAS paramName: "file", method: 'put', headers: { 'x-ms-meta-*': "", "x-ms-blob-type": "BlockBlob" }, maxFileSize: 50, //what's a good file size limit? clickable: true, uploadMultiple: true, maxFiles: 5, init: function () { let sas = null; xhr = new XMLHttpRequest(); this.on("addedfile", function (file) { console.log('File Added!'); console.log(file); }); this.on("processing", function (file) { this.options.url = this.options.url + "/" + file.name; console.log(this.options.url); }); this.on("sending", function (file, xhr) { var sendFile = xhr.send; xhr.send = function () { sendFile.call(xhr, file); } }); this.on("success", function (response) { console.log(response); }); this.on("error", function (response) { console.log(response); alert(response); }); } }; </script> } 

I am not sure if Dropzone sends the Auth header since I've declared an action in the form element. Any pointers are appreciated!

Thanks!

Using Dropzone to upload file to Azure Storage Click here
no image

HoA website with API/Auth/Razor Pages - Asp.Net Core 2.2

Been working on this for a little while and wanted to share. Its a Asp.Net Core 2.2 project that uses API, Razor Pages, and Identity Server for a generic HOA website. I decided to go with razor pages for the front end initially as I figured it would be easier with my prior knowledge of MVC.

Github: https://github.com/vrao1020/netcorehoa

The project is far from complete. There are plenty of features that are missing, a not-so-good looking website, and some design patterns that are not ideal. But I tried to incorporate as many best practices as possible.

The readme on github has instructions on how to configure the project. In order to fully function you'll need to configure a SendGrid API Key and an azure blob storage container. Optionally if you want external auth (Google/FB), you can configure those as well.

Other than that, just login and try out all the various features. There are some features missing that I am interested in pursuing in the future. Especially important is API caching but I am not able to figure that out. Might be a future state project once I do learn more about it.

Questions/suggestions are welcome.

HoA website with API/Auth/Razor Pages - Asp.Net Core 2.2 Click here
no image

First time coder - Is my project attainable?

Hello everyone,

I have never coded before, but I got a great idea for a website that could be a lot of help to a certain gaming community.

What I would like to know is, if it's possible to create a website that acquires information from the game in a way of APIs, and then is displayed on my website. Furthermore, I would like the website to have "categories" that are selected, when the categories are selected the website does not redirect, but rather "slides" to the side to display the categories selected prior.

If you have any problems understanding what I mean, please do not refrain from commenting.

Thanks!

First time coder - Is my project attainable? Click here
no image

Documentation for ASP.Net Core

I'm just curious how most people use documentation for something like Asp.net core. Is reading it from "start to finish" helpful, or is it more helpful to look things up as needed. I tend to do the latter, but am wondering if just reading the entire thing would save me time in the long run.

Documentation for ASP.Net Core Click here
Thursday, March 28, 2019
no image

I'm sorry, but I don't think I can continue with .Net for now.

Hi

For the last few months I've been learning .Net Core. It's been great, I like it a lot, but I can't get a job in it. My previous experience is in Laravel, so for now I'm sticking with that. I spent the last month doing a course using .Net Core & Angular. It was certainly not a waste. I learned how to create JSON apis and consume them using a JS framework.

For now, I need a job, so it's back to Laravel. Maybe in the future I'll work with .Net.

I've asked a lot of questions over the last while in this forum. I want to thank everyone for your help.

I'm sorry, but I don't think I can continue with .Net for now. Click here
no image

Help structuring Git repositories.

This is more of a general "structural" question, I suppose... but I wasn't sure where else to ask it. So here goes.

I'm looking into doing some restructuring of my current project repositories in Azure Devops. My (simplified) directory structure looks something like this:

C:\VS\Projects\LibA \LibA.sln \LibB \LibB.sln \ApplicationA \ApplicationA.sln \ ... 

Where LibA, LibB, and ApplicationA are all applications written in C# and WPF and what have you. Now, let's say that ApplicationA has project dependencies on LibA and LibB, which are then included in ApplicationA's solution. EDIT: LibA could also be a utilities library which is used by many applications. So it's not only a dependency of ApplicationA, but ApplicationB, C, etc..

So, here's the question:

Should I create a single Git "monorepository" in the Projects folder which will include LibA, LibB, and ApplicationA? Or should I create smaller repos for each of LibA, LibB, and ApplicationA?

Right now, I'm using the "monorepository" option as I'm a solo developer and it makes it much easier for me to open up the solution for ApplicationA and, if necessary during debugging or tuning a feature, change LibA and LibB from that solution. Then I can just push the changes from the whole repo to Azure and I'm good to go. The disadvantage to this approach is I lose project level history. A change to ApplicationA will show up as a change to the entire repo, which can make changes difficult to track.

Before, I was using the per solution repo, with a Git repo for LibA, one for LibB, and one for ApplicationA. This made project level history and changes easier to track, but if I changed LibB from within ApplicationA's solution, I had to remember to go into LibB's solution and commit the changes. It was a bit of a maintenance headache.

Plus, I recently had to pull a good portion of my projects from Azure Devops onto a laptop to take with me to work. I had to go through each project, find its dependencies, manually clone each repo from Azure Devops, etc... it was kind of a pita.

Am I doing something wrong here? Is there a better way to do this? I appreciate any input!

Help structuring Git repositories. Click here
no image

How to get Windows Authenticated user in C# Web API sync and async

Hello, I need some helping to solve this issue I'm stuck with at work. We have an Web API in .NET Framework 4.7.1 (It has an API, Application, Core, Test projects). The project is complex and it has code from 10+ different developers. We use Microsoft Dynamics D365 8.2 OnPremise for our data, so anytime we have to do CRUD operations we have to connect with the CRM via their SDK code. The project is hosted on IIS 8.5 Windwos Server 2012 R2 and the front-end is Angular 5 (we use the withCredentials option for API calls). We have Windows Authentication and ASP.NET Impersonation turned on and all other Authetication methods disabled. On our C# code, 99% of the times we connect to our CRM SDK we also need to impersonate the user, we do this by passing the username to get a Guid. This code has been working for a long time, until very recently we found out our code was setting the user incorrectly randomly.

The code that sometimes doesn't work is:

System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name

Most of the times we get the correct user but sometimes it gets the user running the process in IIS.

I also tried:

System.Web.HttpContext.Current.User.Identity.Name

But sometimes this is null

And lastly what I thought the solution was:

System.Threading.Thread.CurrentPrincipal.Identity.Name

This worked for many of my tests. The problem is, after a while (I think if the service gets called inside a Task.Run(() => {}); block it gives an error: System.ObjectDisposedException: Safe handle has been closed

I read the following articles to help:

official Documentation about .net web api: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api

This post talking about a very similar issue: https://social.msdn.microsoft.com/Forums/vstudio/en-US/bca4556e-53f3-427c-a51a-9b955e2395e2/how-to-get-the-windows-identity-through-web-api-2-in-a-intranetlocal-network-setup?forum=wcf#89485c63-352b-434a-862b-8c5f4cec620c

These two post about the Safe Handle has been closed:

https://stackoverflow.com/questions/35460292/retaining-principal-inside-queued-background-work-item

https://stackoverflow.com/questions/16552539/set-thread-currentprincipal-asynchronously

which made me try to write some code like (kept the System.Threading.Thread.CurrentPrincipal.Identity.Name working but still throwing the error):

ClaimsPrincipal principal = new ClaimsPrincipal(Thread.CurrentPrincipal);

// Save Total Payment Stream Payments (Async)

Task.Run(() =>

{

HttpContext.Current.User = new ClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new\[\] { new Claim("[http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name](http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name)", [principal.Identity.Name](https://principal.Identity.Name)) }, "auth"))); Thread.CurrentPrincipal = principal; SaveTotalPaymentStream(opportunityTermId); 

});

Any ideas or suggestions about this would be great. Maybe I need something more complex like creating a custom Principal or maybe I'm just messing up the Task.Run (which I finally thought I had it figured out...)

Thanks!

How to get Windows Authenticated user in C# Web API sync and async Click here
no image

How to keep track of viewed pages in asp.net core api?

Hello, I have a website with backend in asp.net core api and frontend in angular. Website has posts with id like stackoverflow. How do I track unique post visits even by anonymous users? I thought of using sessions but they seem to reset for each request.

How to keep track of viewed pages in asp.net core api? Click here
no image

Issue with connecting tables in MVC

Edit: it looks like I messed up the inline code function in this post, but you should be able to read it anyway. Sorry.

I am making an application for a book donation service. I am having problems connecting tables with lambda expressions when the foreign key for the left table is contained in the right table (BookAuthor contains foreign keys for BookID and Author ID). The following code is supposed to return a view that contains a table of all books and the associated (possibly multiple) authors.

[HttpPost]

public IActionResult SearchResult(int? DonationID, int? BookID)

{

`List<Donation> donationList =` 

database.Donations

.Include(d => d.Book)

.Include(d => d.Book.BookAuthor.Author)

.ToList<Donation>();

return View(donationList);

}

This format gives me errors. A friend told me that I should add a reference to BookAuthor in the Book model like:

public List<BookAuthor> BookAuthorList { get; set; }

and the include statement should look something like:

.Include(d => d.Book.BookAuthorList.Author)

This seems to be the right direction, but I still can't figure out how to make the connections work all the way through to the Author table. I know this is simple, but I am stuck.

Issue with connecting tables in MVC Click here
Wednesday, March 27, 2019
no image

windows 10 developer mod Device Discovery pair grayed out

I am trying to remote debug a uwp application from vs 2017. the target device is a tablet running windows 10 pro v1809 build:17763.379. In developer settings I have enabled device discovery, installed version 1803 of the windows 10 sdk or later and the "Pair" button is grayed out. below it is states: Plesae restart to pair with this device. I have I have restarted and even reinstalled windows and it still shows the same thing. is there any way to get this to work?

windows 10 developer mod Device Discovery pair grayed out Click here
no image

Advice for learning ASP.NET Web API 2

Hey guys, so I am a recent college graduate, and we are starting a project soon that uses an ASP.NET Web API backend with a react frontend. I was wondering if I should learn ASP.NET MVC before ASP.NET Web API? What are your takes? I won't be using MVC at work is the thing, but most resources are see are books like "ASP.NET Web API for MVC developers" or articles on adding a Web API to an existing MVC site, leading me to believe that I should know some MVC first..

Advice for learning ASP.NET Web API 2 Click here
no image

Frameworks

Hi I am beginner at .NET technologies and I am learning WPF framework.

Are there any other jobs in .NET than ASP.NET Developer ? Only job offers that I see are in these technology and most of the tutorials are about it.
I would be thankful for list of most popular frameworks used by companies that knowledge in them would help me land my first job

Frameworks Click here
no image

What would be the best way to implement the following?

Hi

I'm a practise site. I'm using .Net Core & Angular.

The site is based around adverts and users are able to upload adverts. Theres 3 main parts of this. The basic property details, the photos, and the payment.

Would a 3 step form be the best scenario in this case? And should I have each feature above controlled in a separate function. Would each step save to the DB when I press next.

Are they any good tutorials for this?

What would be the best way to implement the following? Click here
no image

polymorphism in Entity Framework (.NET Core)

Anyone has any experience they want to share? I'm currently on designing a project, and I can't really get a good feel about how to do this in a proper way.

The project use case is pretty simple, the user can create their own data fields, and assign values to them. For this, they have DataField<T> and DataValue<T>.

DataField<T> has T defaultValue

DataValue<T> has T value and a refenrece to field for metadata.

So I have a baseclass, DataField and DataValue where T : object.

Since object is not supported in SQL for obvious reasons, I have to write it into something it can understand.

Currently I am trying to create some form of converter - so when I do the modelBuilder.Property(field => field.DefaultValue).HasConversion() i could somehow parse the converter depending on the type.

But I wanted to see if anyone has solved the same problem? Worst case I could always pass it as a string back and fourth, and do some manual conversion, but I don't really like the idea.

polymorphism in Entity Framework (.NET Core) Click here
Tuesday, March 26, 2019
no image

Possible employment opportunity, is this a red flag?

One of the questions I always ask a potential employer is, "If you need to add a new field to the database and to the web application, how many code (.cs) files do you need to modify?" Today the answer was 10. My gut tells me to stay far away from this one, especially since my current job is really pretty good.

Possible employment opportunity, is this a red flag? Click here
no image

VS2017 help drawing lines, circles, and boxes for a web page

I hope this is the best reddit for this. I've been using VS2017 vb for a bit but I'm new to programming for the web. I have built and published some pages with clickable text and buttons no problem but I would like to connect some of those objects with lines and also be able to draw simple boxes, circles and ellipses. Can someone point me to a resource so that I can learn how to accomplish this? Much thanks.

VS2017 help drawing lines, circles, and boxes for a web page Click here
no image

Web Api vs Mvc

I'm about to start an application that has a mvc app and an API. My plan is to have the api built in web API and the app built in mvc. Two separate code bases.

Does this make sense to do or can I combine them into one codebase? The application serves a high level of requests per second via API and basically stores all sorts of data in the database. The mvc part is the portal to manage account and what not.

Web Api vs Mvc Click here
The webdev Team