Visual Studio Unit Test From Existing ASP Web Site - asp.net

Denizens of Stack Overflow, I come before you in hopes of solutions to my current problem, as so many of my questions have been answered by you veritable founts of knowledge. Is there a simple way to create a unit test for an ASP Web Site Project that was already created without requiring the installation of software that requires you to buy them should you wish to continue using them? If there was a way to get the ASP Web Site directory to be treated as a Project, that would solve things very smoothly. There are two methods to accomplish this that might still be viable but that I have given up on are:
Linking the ASP Website Project to a normal Visual Studio Project.
A method I saw online suggested that one could simply drag the ASP.Net Web Site Folder on to a normal VS Project and this would effectively make the Project a copy of the ASP Web Site with all functionality of the Web Site for Unit Testing with the sublime easiness of being able to use Add Reference for the Unit Test Project, so much more simple than what I've encountered with Add Service/Web Reference. On a similar note, there was a website that suggested adding all the content of the ASP Web Site into the Root of a project. Neither of them worked for me, but I might have been made a mistake in my interpretation of the instructions.
Once I gave up trying to get the Unit Test to Add Reference to the ASP Web Site, my next approach was to link a Web Reference to the Unit Test. At first I tried placing the http://localhost(number)/ of the ASP Web Site in the Web Reference URL, but that didn't work. I saw something that mentioned creating an IIS Site for the Unit Test to reference, but I couldn't make much sense of it.
I've been trying to come up with a wsdl file, and to that end I downloaded WCF LOB Adapter 2013. I don't have BizTalk installed, and after the software provided the message that BizTalk needed for BAM to be installed, I decided against downloading further software. BizTalk itself was already about 660 Megabytes, and for something that is easily a fifth the size of the entire Windows 10 Operating System, I thought that BizTalk ought to be able to run without needing additional specialized software.
I recently discovered that .asmx pages happen to show up when I tried to Add a Web Reference. Instead of giving me an error about connectivity, I received an error stemming from how I had duplicate web.config files in the same solution thanks to trying so many different approaches without success and not clearing them out.
I haven't really looked much into MVCs because the tutorials I go through don't really show how you use MVCs to test the existing code of one of the various aspx pages that my company wants to have Unit Tested.
I think I was having success with [this][2] tutorial, but trying to run a asmx file I created gave me the "Not well formed" error, and my Command Prompt doesn't recognize me as administrator, even though Control Panel says I have admin privileges, which seems to be a large impediment to applying the different work-arounds. However, after using "Clean Solution" several times and deleting other projects from the solution, I was able to run the asmx file after all. However, now I'm stuck on the end of the 2nd step where a batch file (.bat) is supposed to be creating a .cs file in the Bin folder from the .asmx page.
I've gotten to where the Visual Studio Developer Command Prompt is okay with both 'wsdl' and 'wsdl.exe' no longer give the:
wsdl/wsdl.exe "is not recognized as an internal or external command, operable program, or batch file" error.
The contents of my batch file are:
wsdl /l:CS /n:WService /out:bin/wsdlWalkthrough.cs
http://localhost/webserv.asmx?wsdl
Since I'm trying to follow the steps of an article old enough to have graduated elementary school, the syntax may easily have changed. Instead, now I get the error:
'http:' is not recognized as an internal or external command, operable
program, or batch file.
I'm going to make this one into a question in and of itself, because this question was if there was a smooth, simple, cost-free way of setting up a Unit Test for an ASP.Net Web Site, not specifically to answer any of my problems, though such would be received with much gratitude.
[Another tutorial][3] might yet provide me a solution, but I'm not going to hold my breath.
UPDATE 2/29/2016
With assistance from Stack Overflow's ever so helpful ChristiFati, I was able to get through Dimitrios Markatos' Creating and Consuming .NET Web Services in 5 Easy Steps which can be found at http://www.sitepoint.com/net-web-services-5-steps-3/ The article may be over a decade old, but it was still by far the easiest method I came across in my week or two of trying to figure out a way to add a reference, or in this case Add Web Reference that I came across. Two things to be careful of though. The first is that if you copy and paste code directly from the tutorial, you might end up with errors because the batch files you make will have an extra newline character, which caused the Developer Command Prompt for VS2012 I was using to run the .bat file to give me the
'http:' is not recognized as an internal or external command, operable
program, or batch file.
error. Thanks again to ChristiFati for pointing that out for me. Similarly, the instructions for Step 3 give you the following code for a second .bat file:
csc /t:library /out:binGetSuppliers.dll binGetSuppliers.cs
/reference:System.dll,System.Data.dll,System.Web.dll, System.Web.Services.dll,System.XML.dll /optimize
Besides eliminating the white-space and newline characters, the above code should probably look more along the lines of:
csc /t:library /out:bin/GetSuppliers.dll bin\GetSuppliers.cs
/reference:System.dll,System.Data.dll,System.Web.dll, System.Web.Services.dll,System.XML.dll /optimize
But aside from that, the tutorial was a gift from God after all the dead ends I had come across. I'm judging my question answered because the critical issue was being able to give my Unit Test a Reference, whether a normal reference or Web Reference. I do not anticipate any more major difficulties and hopefully I will be able to finally proceed to Unit Testing for one of our ASP Web Sites. If not, this post will be edited to describe the newest problem.
Well, this is ridiculous. I'm able to add the Web Reference just fine, but doing so seems to have done diddly-squat for being able to reference the different .cs pages anywhere inside the ASP.NET Web Site. Does anyone have a way to get a Unit Test to be able to reference code/classes within an ASP.NET Web Site?
UPDATE 3/8/2016
There is a somewhat simple solution to the problem in placing all the functionality of your website into a DLL. Once that is done, right-click on the Unit Test project and select Add Reference. The DLL didn't show up on any of the tabs, but I was able to select the appropriate DLL by clicking on Browse.
Still, if there's any other ways to set up Unit Tests from an existing ASP Web Site where such a DLL doesn't exist.
My Visual Studio 2013 Solution
[2]: http: //www.sitepoint.com/net-web-services-5-steps- 2/
[3]: https: //msdn.microsoft.com/en-us/library/ms731835%28v=vs.100%29. aspx

Related

Options for aspnet_compiler to show all errors?

Our development team has used aspnet_compiler as part of our build process for years to verify our website build. I have noticed that when the process encounters a precompile error, it immediately shuts down. Sometimes, if you fix the issue and restart, it will fail again with another unrelated error that actually existed the first time. I am just wondering if there is a way for it to not "die" and continue the precompile and then list ALL errors that it finds so we don't have to do the iterative process of fixing the errors one at a time.
I have looked at this documentation: https://learn.microsoft.com/en-us/visualstudio/msbuild/aspnetcompiler-task?view=vs-2019 but nothing there seems like it supplies what I am looking for.
do you mean that VS shutdowns, or the external command line build process shuts down? One real problem area is the .net folder called app_code. While you can do a build->compile?
You note that code in that folder does NOT get compiled when you do this. It is only compiled by the build process. Even during debugging, you note that compile errors are not seen (or caught) unless you run (try) the site. As a result? I don't use that folder anymore. The 2nd huge problem is royslen editor code. I have started using that ability's for strings to span multiple lines in - really nice for in-line sql.
However, I find then if you let the web site build, it does NOT support + see and allow such Rosylen formatted code. (so upon run of the site, I get errors). (this might well be due to the web site running IIS 2016 - too old for that new code formatting support (at least in vb.net it is).
So, all I did was create my OWN folder for code and NOT use the .net one ().
Note that you create the folder, but remember for EACH code module, or class you drop into that folder? You have to individual set each to be compiled. (it is the default), but be careful if you import code via add. So you see (and want) to use this option:
So, I of course don't want the source included - and I don't require that since this is a web applicaiton vs a web site.
Thus, I don't get/see/have any runtime compile of my code by the web site compiler process - it all done during the build and even debug process in VS.
And then their is the build for a web site publish. Thus during a deploy build (and publish), I don't get any surprises either.
In other words, the GREAT advantange of a web site applcation is you don't let the web site comile code for you.
You do have to mark the code module as per above.
As a result, this folder behaves like any web form with code behind. The standard debug and build process during development will thus compile all my code - and catach errors.
If you use the built in (and special) folder app_code, then such code only compiles WHEN you run the site, and worse it is the web site that does this compile - NOT VS.
(but of course you publish build process ALSO does this compile!!!).
I need (want) a regular build + compile during the development process to catch and compile all that code (else debugging, and worse compiling means you only can find out issues at web site run time - and that's way too late for my tastes).
So, I don't bother with app_code anymore at all. Now I am lucky, since I am creating (using) a web site application, as opposed to a asp.net web site. (and yes, there is a massive difference).
Asp.net web site = each page and code behind will compile on demand. This choice is perferred for two big reasons:
First, you can update one web page (and code)- deploy that one web page + code. The web server will figure this out - and re-compile that one page on demand. This makes updates and maintains of the site OH SO VERY much easier.
And it also means in most cases that the whole site can be published to a sub-site folder on the hosted web server. And in fact most really cheap low cost web site hosting MUST use this option (web site applications in most cases can't be published to those lower cost .net hosting sites). (thus use web sites, not web site applications).
However, if you lucky, and you have FULL use of IIS and a server dedicated to JUST running your web site? Well, then you are MUCH MUCH better off to go with a aps.net web site application. And this also means you can say setup custom logon providers, and also configure things like re-direction or things like a custom web handier. In effect, this choice means that you as a developer not only have full use of the base starting page, but can directly change/configure the web server to your liking. This includes the base web config.
This choice is often not avaible on lower cost hosting plans. it requires that you have full IIS services, and IIS services is running your whole site, and the base starting page is your site. (you can publish to root on web hosting, but you still using THEIR EXISTING copy of IIS services, and you can't control things like creating a custom authneticaion (logon) provider. And you also can't create re-directs (custom one).
However, in both cases/choices app_code folder behaves like a compile on demand folder. Thus bugs and issues in that folder will thus not be found until you run the web site, or do a full deploy that then does the full site build. and as noted, you can't use Rosylen eiditor souce code features, since the web compiler might be a few verisons old. You be running the SAME .net version, but the older compiler does not support source code formatting with the new Rosylen editor features.
Since a web site application will compile everything (whole site) in your application down to a single .dll (and of course referenced libraries), then then you don't have on-demand occurring by the web site compiler (after you deploy). Of course the publish wizard does have options to combine all those .dll's into one - not a big deal either way.
So, the one exception is app_code. So I just stopped using it, and I see little reason to use that special .net folder anyway.
So, it was never clear if VS is shutting down, or your build process is seeing errors not being caught during the regular development cycle with VS - if this is your issue, then the above approach should solve the problem for you.

Custom path for folder

How to add a reference to the App_code directory for a sub-folder in web project
I have been given a task to take an older, grown piecemeal over a decade internal website and bring it "up to speed". I have run into a problem in ASP files to "see" the app_code directory. Essentially the current site is a collection of folders that work(ish) that I am trying to collect together as a single project with the LEAST number of edits.
When functionality was superseded the old code was moved into a "deprecated" folder. The code files now reference object in the app_code directory which is not visible to the file from it's new location. The easy answer would be to copy a version of the class file into the "deprecated" folder but that seems more like a hack then a solution. The same for copying the App_code folder to a location visible to the ASP file.
My thought was to add a reference ("reference path") somehow to the deprecated folder pointing to the app_code folder but I cannot find how to do that.
A couple of other notes: 1) the site was built by using text editors and "freebie" editors - it has never been compiled or run in-total through VS before. 2) once I get it built and checked into TFS I am going to remove the deprecated folders. 3) I am working under some stipulations to satisfy concerns arising from actions of prior developers.
Code like:
tr.Controls.Add(GUI.GetTableCell("Salesman", 0, "Center"));
Works well when the ASP page is at the same directory level as the App_code folder ("GUI" is a class with a "GetTableCell" function).
If I can add a folder reference I can solve all of the remaining problems I have with this step in the project.
EDIT
Let me re-ask it this way. The image below is a composite of what I am facing in VS2017. I have such a feeling that a light bulb is simply not turning on for some reason. How I have done this so far is pulling down the current website from our internal Win2003 web server and am trying to convert it into a legit web project. I have done many of the steps necessary but there are 47 instances of the problem visible in the composite image from Visual studio
The code behind from SubscriptionEditor.aspx pictured lower right. Has reference to a namespace which is in the code files in the App_code folder (neatly hidden). This site functions fine but I am missing something to make it work from within VS. I am just at a loss.
For anyone that finds this I want to point out that there are a number of excellent answers available for similar questions here and i have looked at dozens and tried them all. The simple answer to my problem is "you can't".
How to convert ASP.NET website to ASP.NET web application came close
The problem being the source is just a collection of ASP files - it follows no project or template.
Namespaces are super important in development and the clever ways that prior developers found to breach that discipline can not be fixed through VS. It is simple a lot of bloody knuckle corrections of code and scope issues til the errors disappear.
The limitations put on me prohibit any solution. I will end up advocating not "saving the code" unless they are willing to put in the investment to heal the most egregious foibles. So if you see me in the "available for contract" sites then you know how that conversation went.

How to get MSBUILD to precompile .aspx pages

I am working to setup a build server using Team City to build and deploy asp.net web applications to a staging site with transformed web.configs automatically. Everything is working except that the code that ends up on the website (the aspx files) have the HTML in them when you open them in notepad.
Before all of this I was using web deployment projects with websites, and the code was compiled. If you opened one after it was deployed, it said it was a marker file.
I have tried some tutorials on how this process should work, but the code always ends up in an editable state (the html).
My question is:
What do I need to do to get MSBUILD from the command line to ultimately have precompiled code on the webs server?
Any suggestions, links, pointers, or ideas would be very helpful to me.
You need to invoke the aspnet_compiler tool to do this. There are some limitations or complications depending on exactly what you need to do for things like strong-naming. The MSDN article here has pointers.
I used to have MSBuild project steps that did this, but we decided to drop precompiling because our clients want to integrate our product into their internal portals, and precompiling made things complicated for them.
Are you using MS Web Deploy? I use it regularly for automated deployments from my Team City Build server to dev, staging, QA, etc. And I'm transforming configurations as well.
If you want to check out this alternative you can follow the excellent guide by Troy Hunt:
http://www.troyhunt.com/2010/11/you-deploying-it-wrong-teamcity_26.html

MSBuild fails on temporary ASP.NET files

I'm trying to set up Jenkins with MSBuild plugin. I got Jenkins to check out solution from repository and run a build. But every build fails with several
error CS0433: The type 'CustomControls_WarningPopup' exists in both (...)
WarningPopup is an .ascx defined twice - once per web site project, and there are two website projects in the solution. The thing is this never rises any problems when compiling whole solution from VS2010. Should I run MSBuild with some specific parameters to make it behaving like VS?
I already tried moving Temporary ASP.NET files to custom folder (like in this answer) and it didn't help. Probably I'm missing something obvious to CI experts here...
I found the cause. There was another UserControl in second project which accidentally inherited class of the same name. Both controls were in the same directory and nor Visual Studio during compilation nor ReSharper saw anything wrong with this. Moreover MSBuild threw errors in both Web Projects despite the duplicated control was in only one of them. The funniest part is that this situation was like this for months and never rised any problems...
Anyway changing class of this control solved the issue and finally automated build succeeded!

Automating finding compile errors in ASP.NET pages

I have an ASP.NET website where the pages call a few components in DLLs. I need to change the signature of a method in the component, and short of doing a text search, don't know if this will break any pages or not. IMO, this is the weakness of web programming -- you don't get the benefit of a compiler telling you about syntax errors.
But it doesn't need to be so. Does anyone know if there is a way to run a spider over a website watching for compile errors, or perhaps some tool that would compile all the .aspx files in a folder structure looking for compile errors?
This is merely for syntax checking -- not to actually pre-compile the website.
EDIT It looks like aspnet_compiler is being recommended. I don't use Visual Studio projects for the website -- it's grown over time with my own templating system (back before Master Pages were available). So something that would run aspnet_compiler over all the files in a folder might work...
There is a flag that you can put on your project that tells it to compile all the aspx files when the project is compiled. It adds time to your build, but it can sometimes be worthwhile. See http://mikehadlow.blogspot.com/2008/05/compiling-aspx-templates-using.html
Also, Resharper is really good at finding references to methods, even in aspx files. So if you use Resharper to rename a method, as long as your solution includes the web project, it'll find and rename that method in the aspx files, too.
This is one of the many reasons we use development tools like Visual Studio in the first place. The single easiest way to do what you're asking is to develop with an IDE that DOES compile and check for errors, even ifyou choose to publish teh un-compiled code.
Since Microsoft offers Visual Web Developer for free, there's really no reason to NOT use it.
The compiler will automatically catch and any report any errors in your .cs source or code-behind pages. Your assumption that the compiler won't catch syntax errors (such as getting the arguments in the wrong order when calling a method, etc) is incorrect - that's one of the primary benefits of using a compiled language. If you're experiencing something that contradicts this, please post some code.
If you're concerned about errors in the ASPX files or in your views (if using MVC), you can have the IDE precompile ASPX files, as well.
See this article for more information.
I turn this off most of the time since it slows down compilation, but I use it before deploying a site as an extra verification step.

Resources