Error when attempting to run application - asp.net-mvc

I am moving an asp.net web forms application over to mvc. I want to re-use as much code as possible so I decided use my existing linq to sql library.
I am new to MVC, but have viewed numerous videos from PluralSite for direction.
I created a model library to work between the linq to sql and the web api. I had read that I needed to do this because of linq to sql's complexities.
I have also created a web api and started adding linq queries to the controllers.
public IList<Model.vSchedulesFill> GetCustomerSchedules(int id)
{
var sch = from p in _context.vSchedulesFills
where p.CustomerID == id
orderby p.SchedStartDateTime descending
select new Model.vSchedulesFill(p);
return sch.ToList();
}
I tested the above query using Fiddler and the result were good, so I tried to run the program and received the HTTP Error 403.14 - Forbidden error.
The database is on a remote server, but I am running the app in debug mode on my local machine.
The error mentions no default document or IIS directory browser not turned on.
I looked up the default document and saw a lot about adding the web api help file. So I went to NuGet and installed it, but the application still will not run.
I have looked all over for solution to this problem...
What am I doing wrong and what do I need to do to fix this?
Thanks for your help
Added
<directoryBrowse enabled="true" />
to web config under section system.webServer.
Can view directory but that's not what I needed. I basically wanted to be able to run the program and then type in the path in the address bar, like I did in Fiddler.
I didn't remove any of the default layout forms included in the web api project.
Thanks

Related

How to debug MVC web app with rest api controller backend? [duplicate]

In VS 2012, I am attempting to create an MVC 4 web application with jQuery calls to a Web API project. (Other devs will be consuming the API with our current, native app, and probably adding to the API in the future.) So I have one project that is the Web API, and another project that is the MVC 4 website. I can only set one of them to run, and they use localhost:xxxxx.
How do I debug changes to both? For example, let's say I add a new API path /api/customer/get and then a new jQuery ajax call to that path and do something with the resulting JSON. I've changed code in both projects and want to follow it end-to-end; how do I launch both? How do I debug both?
Just to be clear, the MVC app isn't making server-side calls to the API, I'm using MVC mostly to be able to easily use bundling, minification, and (hopefully) pre-compiled Handlebars templates in .NET; the API calls are coming from jQuery. As I am still relatively new to these technologies, alternate suggestions are welcome.
Thank you in advance.
I had the same problem and have found a solution from here:
forums.asp.net
The fix is to do the following:
In your solution file, click properties go to the Startup project node (if it is not already selected)
Next select Multiple startup projects. Select your website and your webservice and in the Action column make sure both of them have "Start" selected.
Now when you debug your website and put a break point in your webservice, it should hit the break point.
Coming late to the party but in case anyone else is looking for a solution, this is what was best for me: Set the Api project up to be the starting project (I needed to limit to one startup so that I could flip between browsers more easily). After firing up the service project, right click on the web/ui project and select debug, start new instance. You'll have both running and you'll seamlessly step from web to api.
I had a similar problem with my web api project. My solution consisted of an angular front end with 2 web api projects on the backend. One web api project handled "authorization" and the other handled "resources". I used the following tutorial by Taiseer Joudeh as a starting point:
http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/
Breakpoints worked on the "authorization server"... but not on the "resource server". I compared the packages from the two projects to see what was different. Once I added "Microsoft.AspNet.WebApi.Cors" to the "resource server" project, the breakpoints starting working.

Routing not working for MVC application under a Webforms website?

I'm creating a Reporting application in MVC that I want to use in multiple websites. I want to be able to simply create an application in IIS under each of the consuming websites and point them to the same directory where the Reporting application is located.
When I tried doing this in an MVC website it worked fine. However, when I tried adding this application under a Webforms website I got a "403.14 - Forbidden" error because it's trying to use the Static File handler.
How can I correct it to use the right handler to route to the Home controller?
The problem was that the base website's app pool was using the 2.0 clr. Once I realized this I made the necessary web.config adjustments and recompiled the app to run under the 4.0 clr and then everything worked.
I also have a bunch of <location path="." inheritInChildApplications="false"> sections. Not sure if those are necessary now, but I'm leaving them since it works and the sub app doesn't need to inherit anything from the base website.
This post (Expression of type 'System.Web.Mvc.MvcWebRazorHostFactory' cannot be used for return type 'System.Web.WebPages.Razor.WebRazorHostFactory') also helped me in trying to get the web.config stuff sorted out while converting from 2.0 to 4.0. That was kind of a pain because there were several errors I had to work through, but I think I mostly just had to remove some sections of the web.config that are no longer necessary (because they're now included in the machine config).

External HTTP requests from MVC application fail

I'm having problems making HTTP requests from my MVC.Net Intranet application. I encountered the problem when using the RestSharp library, but have boiled it down to a much simpler repro.
The following code:
using (var client = new WebClient())
{
var contents = client.DownloadString("http://www.google.com");
}
Will run successfully (on my local machine, debugging from Visual Studio 2013) inside a console app.
Will run successfully (on my local machine) from within LinqPad
Times out (on my local machine, debugging from Visual Studio 2013, hosted with IIS Express) when run from a controller action in a MVC intranet app.
This may be a duplicate of Not able to connect to website URLs from Asp.Net WebApi Action Methods (and is where I got the boiled down proof-of-problem code above) but in my case this is all running locally on my computer and it works fine from a console app.
I've tried various different code snippets, including using HttpClient instead of WebClient:
using(System.Net.Http.HttpClient c = new System.Net.Http.HttpClient())
{
var msg = c.GetAsync("http://www.google.com").Result.Content.ReadAsStringAsync().Result;
}
But I get the same, or similar results and errors in all cases. For the code above, I eventually get a 'TaskCancelled' exception, with no Inner Exception.
I've also got colleagues to try the same on their own machines, with the same results. I have read some similar questions, where people talk about problems with deadlock due to the SyncronizationContext in ASP applications, but that seems to relate to asynchronous calls. Here, as far as I can tell, everything I am doing is synchronous so I can't see how that applies.
In reality, I'm using the RestSharp library, which I've used to create a client library of my own for talking to the facebook API. I had tested this from a console app test-harness, which worked perfectly, but it all times out when I start using it inside my MVC app.
Has anyone seen this before and have any ideas on where I'm going wrong, or what the issue might be?
Is it to do with locking / deadlocking?
Could it be to do with permissions / proxy? We have quite strict web proxy rules at work, which are obviously configured correctly for my user. Could IIS Express hosting be changing the permissions or the user assigned to the app? The IIS Express service is running with my username at present.
UPDATE
I have tinkered some more and tried accessing an internal company web page instead of an external page (Google). I find that this works immediately - so that suggests strongly that it's to do with permissions or proxy settings.
Can anyone explain why this would be different between a console app and a MVC application?
UPDATE 2
I've run Fiddler to check the request and see what was happening to it. When fiddler is running the request succeeds.
This makes me think it's some strange permissions issue possibly? As a security measure, our login accounts don't have admin rights, instead we have a separate non-interactive account with admin permissions we can use to run apps that need admin rights, or install software etc. To get fiddler to capture traffic, I have to run it with this account.
Still, the calls work fine with my own normal account from the console app or LinqPad - so I guess my question becomes:
For an asp / MVC app locally hosted via IIS Express (Visual Studio Debugging), what user will this be run as, and what differences (if any) would there be to a console app?

Deploying ASP MVC 5 App with IIS 7.5

I try to deploy ASP MVC 5 app in virtual directory (without creating new iis application)
I use IIS 7.5
I already put
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true" />
in web.config file.
But when i go to app url with IE browser it shows me just directory listing like in screenshot below
Is there a way to deploy MVC 5 in virtual directory and make it work like usual MVC application?
You need to convert the virtual directory to application. Right click on it in the IIS management console and choose Convert To Application.... Also make sure that the associated application is configured to use Integrated Pipeline Mode.
I solved this problem earlier in my production environment by checking the directory pointer in IIS. Apparently when I unzipped the deployed site from one server to the next, the zip utility made an extra level, so IIS was pointing to /MyProject when the files were in /MyProject/MyProject. I had a little better clue though, you have Document Browsing enabled based on that screen shot, make sure not to do that in production. I set the site to log custom errors and got a 403.14 response, from there found a blog on my mistake. You need to setup the environment to find the specific module that's failing, I think something to do with trace routes, idk. I'm a software developer that always gets forced into doing devOps; was googling my own problem and thought I'd throw you a line. Without a specific error message, all I can tell you is IIS is not connecting to .NET; something is not configured correctly. Turn off directory browsing, google how to get good error logs back, and let us know the status code so we can help you: 403.14, 401, 500, 404? Also give us the module that's failing. If it's the last one on the handler list, guess what, IIS isn't connecting to the app, which I suspect is your case.

deploying AntiforgeryToken Error

I am working on an ASP.NET MVC application on my local machine using the Visual Studio 2012 built in IISExpress. After a significant progress I decided to make it available across the Local network, I created a virtual directory from visual studio. I can log in but when I try to register a new user. I get the error below.
The anti-forgery token could not be decrypted. If this application is
hosted by
a Web Farm or cluster, ensure that all machines are running the same version of
ASP.NET Web Pages and that the configuration specifies explicit
encryption and validation keys. AutoGenerate cannot be used in a cluster.
I generated a new machine key from (ASPNET resources) and placed it in my configuration file but is not still working.Can someone suggest a solution?
One thing I noticed is that this error could be caused by having multiple #Html.AntiForgeryToken() pieces of code on the page. I had 2 on my page and once I removed the second one, this error went away.
You should be adding a machine key that is unique to the website/machine combination. It is this machine key that the token generator uses. This means that you should generate a machine key (which you can do here (dead link-beware) and add it to your Web.config file on your machine. You can find more information here:
http://msdn.microsoft.com/en-us/library/ff649308.aspx
You can also do this directly from within IIS: http://blogs.msdn.com/b/amb/archive/2012/07/31/easiest-way-to-generate-machinekey.aspx
There I was able to find the answer http://iamdotnetcrazy.blogspot.ru/2013/08/how-to-solve-anti-forgery-token-could.html
In my case this was an issue with the load balancer not having session persistance turned on for the site.
I was having a page which was using the partial view. Both contained #Html.AntiForgeryToken() and on removing the #Html.AntiForgeryToken() from the partial page, it worked.

Resources