MVC 3 with IIS Express Odd Redirecting behavior - asp.net

I have a simple MVC 3 webapp that's been giving me trouble lately - and nowhere I look seems to have any information that can help me. I've been having two issues that seem related in how they're behaving.
In development, it starts fine on localhost (http://localhost:53268/) and works for the index, but doing anything outside of that has IIS returning a 404, ignoring any redirecting to what should be a proper URL.
Now, in production, it behaves differently. Again, the index page works fine, it has the proper URL, but any other page (either through a link or going directly to the URL) redirects to localhost (https://localhost/). The odd thing is for me, it's working as it should be on development (giving me what I should be seeing when I usually get 404s), but on other machines it gives absolutely nothing.
I had inherited this from a co-worker who had since left, but had given me the advice of using IIS Express to get it working from the development side. This was already working in production before, but now... not so much.
EDIT
The global.aspx file
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ElmahHandleErrorAttribute());
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
EDIT
Got it figured out. Turns out there were some issues with Web.Config and the server it was on getting the wrong one. Works fine now.

Related

No type was found that matches the controller named 'help'

I have been following this guide to add a help page to document my Web API project. My Controller is named HelpController and I have a route that I am trying to use to map the Index action to /Help. This is the only MVC controller in the project. Because the rest are Web API controllers, we removed the "/api" prefix from the default route in WebAPIConfig.cs.
The HelpController:
public class HelpController : Controller
{
public ActionResult Index()
{
var apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
return View(apiExplorer);
}
}
And route config:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "help",
defaults: new { controller = "Help", action = "Index"});
}
}
In Global.asax.cs
protected void Application_Start()
{
// ..
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
// ..
}
But when I try to navigate to /help in the browser I get the following error message.
<Error>
<Message>No HTTP resource was found that matches the request URI 'http://localhost/ws/help'.</Message>
<MessageDetail>No type was found that matches the controller named 'help'.</MessageDetail>
</Error>
EDIT: The message contains /ws/help as the application is hosted at localhost/ws in IIS.
Does anyone know what could be causing ASP.NET to not find my HelpController?
UPDATE: If I change the order of RouteConfig and WebApiConfig registration calls in Application_Start I get a 404 instead.
protected void Application_Start()
{
// ..
RouteConfig.RegisterRoutes(RouteTable.Routes);
WebApiConfig.Register(GlobalConfiguration.Configuration);
// ..
}
The request for help is being matched by Web API's route as you have removed api from its route template. if a request matches a route, further probing is not done on rest of the routes.
You probably have the default order in Global.asax where Web API routes are registered first and then the MVC routes. Could you share how your Global.asax looks like?
EDIT:
Based on your last comment, if you install HelpPage nuget package, make sure that the order in your Global.asax looks like this:
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
I had this same error today.
<Error>
<Message>No HTTP resource was found that matches the request URI 'xxx'.</Message>
<MessageDetail>No type was found that matches the controller named 'xxx'.</MessageDetail>
</Error>
In my case after 2 days of debuging I finally solved it by setting the microsoft report viewer dll to "Copy to local". Makes no sense to me how it could be related, but maby this will help someone.
I was getting the same error:
Error><Message>No HTTP resource was found that matches the request URI 'http://localhost:53569/api/values'.</Message><MessageDetail>No type was found that matches the controller named 'values'.</MessageDetail></Error>
By default when I created new controller in asp.net web forms application, it was like this:
ValuesController1 : ApiController
I just simply removed "1", and make it:
ValuesController : ApiController
And it works, don't know if it is a bug or whatever, but it made big trouble for me.

Issue ASP.Net URL Routing - the route on which I redirect 2nd time is not correct

I am facing an issue in ASP.Net URL Routing. Following is the Global.asax code:
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Project", "{dealname}/{city}/{projectname}/{projectid}", "~/projectpage.aspx");
routeCollection.MapPageRoute("Home", "home/{dealname}/{city}", "~/index1.aspx", true, new RouteValueDictionary { { "dealname", "property-for-sale" }, { "city", "Ahmedabad" } });
routeCollection.MapPageRoute("ProjectType", "result/{dealtype}/{searchstring}", "~/result.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
When I open URL of the site Route "Home" is perfectly working. But when Redirect to the Route "ProjectType" using Response.Redirect, the "home/" portion of the previous URL remains as a result, it remains on the same page and in the URL it's showing /home/result/{dealtype}/{searchstring} instead of /result/{dealtype}/{searchstring}.
Please guide me what is missing or what should be done to resolve this issue.
Thanks,
Munjal
I found out the solution. Instead of using Response.Redirect(), use Response.RedirectToRoute(). This function is specifically used when implementing URL Routing.
Reference Link: http://msdn.microsoft.com/en-us/library/dd992853.aspx
Thanks,
Munjal

asp mvc not show route and title in url after deploy

I am developing a website with ASP.net MVC3.
I have built it up and ran normally at local. Then when I deploy it with IIS 7.5, the site can display. All functionality works except the url is not changing when I switch in between actions and controllers(the url always shows "www.mysite.com" not "www.mysite.com/home/action"). Also, the title of pages are not shown. Instead of my slogan, it shows the domain url like www.mysite.com on the page title of browsers.
I followed the official deploy instruction of ASP.net with IIS.
Is there anyone knows what's the problem? Thanks in advance.
Here is my code for the Global.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ImageWall", // Route name
"ImageWall/{action}/{id}", // URL with parameters
new { controller = "ImageWall", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
Regards the title, in your views, do you use something like
ViewBag.Title = "Some Title"
In the HTML source of a page on your site, does the <title> element get populated? If not, then that could explain why you do not see the title.
Regards the url, can you post the code of your global.asax.cs file please (or global.asax.vb if you're using VB.NET). That is where route configuration can take place, so seeing that could help us explain why the url does not a show.
I have found the problem.
The problem is caused by the settings of Domain.com. As I am a fresher to deploy, I didn't set the "A Record" for all my domains. I just set the redirection to my IP.

Publish my MVC3 site on localhost

I am trying to publish my mvc web site on the localhost through visual studio but the problem is when i browse to the the localhost in the browser it gives me a directory listing page. here is the screen shot of publish dialogue. Can somebody please guide me through the process i have been searching for the whole day but couldn't get it to work
here id the global.ascx
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Facebook",
"XdReceiver",
new { controller = "Account", action = "XdReceiver" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
Do you have a requirement to use WebDeploy for your publish? If not, you could create a directory and point IIS to that directory and change your publish method to File System.
IMO, WebDeploy is overkill for localhost deployments if you're using it simply for testing. WebDeploy becomes very useful for network deployments since it compares the contents of the directories and maintains which files have been updated and which ones have not.
just create a empty site using IIS. and then publish the site in visual studio using the File System publish method.
Try checking the "Mark as IIS application on destination" option.
Also, try publishing to Default Web Site/Rental and accessing it as http://localhost/Rental.
If you installed IIS after installing .NET, did you run aspnet_regiis -i? And could you please post your Global.asax file with MVC routes?

Receiving 404 error when trying to get to MVC application in local IIS

I have an existing web site setup on my local IIS built with web forms setup in this format:
-MainSite
-Application1
-Application2
-Application3
There are separate applications in each location, so MainSite has an application, and Application1 has another application and so on. This way development on different applications in the same site can be broken out. I want to add a new Application that is MVC instead of web forms. I created a new MVC project which runs in Cassini just fine, but when I create a new IIS application for it and point it to the MVC project I am unable to navigate to it in the browser and get a 404 error. I am expecting to be able to navigate to http://MainSite/NewMVCApplication/. I want the root of NewMVCApplication to point to my Contacts Controller.
My global.asax in the new MVC application currently looks like this:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Root", // Route name
"", // URL with parameters
new { controller = "Contacts", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
}
Make sure NewMVCApplication is running in an integratedMode pipeline application pool.

Resources