I have an MVC project that works fine when in VS.
But when I publish it and use it in IIS, a search page always gives this result:
No web page was found for the web address:
My controller has these two options:
[HttpPost]
[Route("[controller]")]
public IActionResult Search(SearchModel model)
&
public IActionResult Index(SearchModel model = null)
The page will load normally but once I perform the search in IIS it just wont work, any idea why this would be when VS handles it just fine.
Just incase this incredibly rare issue occurs for someone else!
I have finally managed to correct the fault by slowly removing code from the error page until it finally loaded.
The problem was making a call to a method in a class that made use of HtmlAgilityPack and Fizzler.
For some reason MVC cannot handle it, and not a single error of relevance was offered anywhere!!
I uninstalled both and found an MVC specific version in nuget packages.. Lord!
Related
So here's my situation...
I have a unit test project wherein I instantiated a Web API controller. Both the unit test project and the ASP.NET Web API project are in the same solution. The Web API controllers' implementation includes one part that is to call HttpClient.PostAsync to another Web API that is in another solution and is deployed to local IIS.
The project of the caller method and the project that is deployed to IIS are both opened in Visual Studio (2 VS windows opened). I have already copied the pdb and all from the solution that is deployed to IIS to the bin/debug folder of the unit test project.
But everytime the control goes to the PostAsync call, when I pressed F11, it doesn't get into the code that is opened in another VS editor.
May I know how will I be able to achieve this?
Unit Test project:
[TestMethod]
public void TestController
{
TestController t = new TestController();
t.Get();
}
TestController:
[HttpPost]
public ActionResult Get()
{
//assume that HttpClient.BaseAddress was already set in constructor
client.PostAsync("/testapi/getdata");
}
Controller in another solution, deployed in IIS
[Route("testapi/getdata")]
[HttpPost]
public ActionResult Get()
{
//implementation here
}
The problem is that you are calling the API using Post, but your Api is accepting only Get
client.PostAsync("/testapi/getdata");
///Controller in another solution, deployed in IIS
[Route("testapi/getdata")]
[HttpGet]
public ActionResult Get()
you have to change client.GetAsync maybe or you can use post but change api.
and when you use async you have to use await or Result, but await is preferable
awant client.GetAsync("/testapi/getdata")
//or
client.GetAsync("/testapi/getdata").Result;
///Controller in another solution, deployed in IIS
[Route("testapi/getdata")]
[HttpGet]
public ActionResult Get()
// but should be
public async Task<ActionResult> Get()
UPDATE
you have updated your question, but after this it looks even more strange
[Route("testapi/getdata")]
[HttpPost]
public ActionResult Get()
for getdata without any input parameters and null request body you select HttpPost.
client.PostAsync("/testapi/getdata");
It will not be even compiled, since post needs content.
I am wondering what is API controller like. Does it have an attribute route too? Or all your code is just a fake?
You cannot share debugging instances in Visual Studio between 2 solutions when the other solution is not in debug mode.
Run the other solution within Visual Studio itself (in a separate instance, on a different port) & place a breakpoint where the request should hit - Get().
Make sure the API is being run in debug mode and not release mode for any breakpoints to be hit.
The request from solution 1 should then hit the breakpoint in the controller in solution 2 and allow you to step into the request as needed.
P.S. you don't need to deal with PDB files for this - your issue is how you're running the second solution/API.
I have an application developed using C# under Net 5. The issue I am facing is only happening when I access the site remotely as it is already published on IIS, otherwise running it directly from Visual Studio seems to work rather fine.
Below is my "Index" View :
<form method="post" asp-action="GenerateOTP" asp-controller="Home">
Below is the controller:
[HttpPost]
public ActionResult GenerateOTP(OTPData mydata)
{
...
return RedirectToAction("EnterOTP", "Home");
}
Basically the app should automatically redirect to the page "EnterOTP" after performing the commands on the "GenerateOTP" method. Instead on production the page redirects to Home/GenerateOTP on the browser and this would definitely not work as the page doesn't exist.
Any recommendation on what I am missing.
#PanagiotisKanavos Thanks for the tip. Checked my Event Viewer and found out the issue was on my ApplicationPool Identity. It wasn't authorized access to that directory. Changed it from ApplicationPoolIdentity to LocalSystem and all works fine right now.
I'm trying to make an ASP.NET Core Web application to host IdentityServer and use it as my main authorization service, shared across many other apps.
I followed the quick start tutorial that consists in installing a nuget package (called IdentityServer4) in an empty MVC application template.
Everything is working fine on my machine (debugging with VS2017 & VS2019 preview).
I tried to publish the app as an Azure App Service like i did with previous apps and worked fine.
While debugging, if i open a browser and type "https://localhost:44316/", i see this page:
But navigating to "https://xxx.azurewebsites.net" (that is where i published the app) is not working. Responding 404 not found.
Just to add some detail, if i navigate this "https://localhost:44316/Account/Login?ReturnUrl=%2Fgrants", i obtain the correct login page and doing the same on Azure produces the same result.
So, essentially, looks like it's working, except for the home page.
I'm not an expert in routing with ASP.NET Core, so don't blame me if my question is not so smart.
So, essentially, looks like it's working, except for the home page.
This is actually the intended behaviour - If you take a look at the implementation of the HomeController provided in the IdentityServer4.Quickstart.UI project, you'll see the following implementation (source):
public IActionResult Index()
{
if (_environment.IsDevelopment())
{
// only show in development
return View();
}
_logger.LogInformation("Homepage is disabled in production. Returning 404.");
return NotFound();
}
When running in Azure, the application's environment isn't set to Development environment and so returns 404 NotFound, as shown above.
usually I code for Mozilla Firefox/Chrome. My client reported a bug when working on Internet Explorer. I checked it and he was right. I noticed that Internet Explorer doesn't run my controller method, it just shows me its view without doing thing in controller method. It causes a problem, because I have session clearing when going into my "Create" method. On Mozilla/Chrome it works as expected - controller method is invoked. How to prevent that behavior? It is connected somehow with caching? I tried to add [OutputCache(Duration=0)] on whole controller for testing (as I found on the internet) but it didn't work.
I helped myself.
I added an annotation on whole controller:
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
I also had to remove old cache from IE.
I know that similar questions have been asked here but I am nevertheless having trouble getting it to work.
I am writing an html 5 website using ASP.NET MVC 3 that is targeted to mobile devices.
I had previously asked another question about how to store and retrieve expensive data and decided to go with Session variables.
I finished the app and started internal testing and all was well until we realized that the web site is not working on some iPhones. After researching I came to realize that Session variables depend on cookies and that some iPhones ship with cookies disabled by default.
Until I come up with a better solution, I want to check to see if cookies are enabled and redirect to an error page.
I was hoping that ALL of the code to do this could be written in a base controller but I could not figure out how to do it. So instead I put the following code into the base controller...
protected void WriteCookie()
{
Response.Cookies.Add(new HttpCookie("cookie_test"));
}
protected bool CookieCheck()
{
return (Request.Cookies["cookie_test"] != null);
}
Then in a home controller I am calling the WriteCookie method before loading the view. The view itself has javascript that is redirecting to one of two controllers / actions. The controller / view that will usually be called (because html 5 could not resolve the location) is the ChangeLocation / Index controller/action. In there I have the following code...
if (!CookieCheck())
{
RedirectToAction("CookieRequired", "Error");
}
Even with cookies disabled it is not redirecting to the above error page.
So what I am looking for is a best practices approach for solving this problem with a fairly complete code sample.
My home controller is pretty much ALWAYS redirecting to another page (in the javascript not the controller). I have a total of about 4 controllers with a total of about 6 actions.
My ideal would be to solve this problem completely in the base controller. If not that then I would love for it to work no matter what valid URL is typed (in other words, even if the web app is not entered through Home / Index). If not that then at least I want what I have shown in my code to work (set the cookie in Home / Index and check for it in the other controller / actions) and work even if the redirect is happening in a controller.