I have a web application that I'm using a custom route to route http://domain/MyMVCSite/MyPage.ASP to route to a controller/action of my choosing.
I'm not TOO familiar with custom routing, but as it stands, this is my custom route:
routes.MapRoute(
"Page",
"MyPage.ASP",
new { controller = "KTASP", action = "KTASP", id = "" }
);
As I said, this routes:
http://{domain}/MyMVCSite/MyPage.ASP
I would like for it to route:
http://{domain}/MyPage.ASP
Is this possible? How would I tweak the custom route?
My MVC site is being deployed as a virtual directory of a website on IIS6.
This isn't possible without IIS configuration to rewrite the requests for "http://{domain}/MyPage.ASP" to go to your virtual directory.
Currently, your web application will never get considered, because that page is not within the virtual directory it is mapped to in IIS. You need to either have routes (and a web app) at the root of the web site, or use URL rewriting to forward it to your vdir.
Related
I have a website made in C# (http://my.site/), and now i have a Web API project that i want to merge to that domain and listen on http://my.site/api. They are 2 separate projects in VS2013.
I tried uploading Web API to /api/ subfolder in the server and map a MVC route to it (WebApi route in WebApiConfig.cs still routeTemplate: "api/{controller}/{id}"), expecting the default (Home) MVC controller to respond in /api/. The MVC route (in WebAPI's MVC routing):
routes.MapRoute(
name: "Default",
url: "{controller}", //Also api/{controller}
defaults: new { controller = "Home", action = "Get", id = UrlParameter.Optional }
);
But i get "Server Error 404" when accessing http://my.site/api/ / http://my.site/api/home etc. I don't understand the nature of this error (most likely settings in the C# website in root folder), but one would think IIS would serve the default app inside the /api/ folder.
So i want to know if adding WebApiConfig.Register(GlobalConfiguration.Configuration); to Global.asax (Application_Start()) and adding WebApiConfig.cs to App_Start folder of the C# project (and using the respective namespaces) will solve the issue. This seem too simple to be true so i doubt it will work, so i need someone to point me in the correct direction, or i will commit the sin of going back to asmx.
If not, how can i have C# website and a WebAPI on same domain?
You are wanting to run two separate dlls (each with their own routing rules) within the same bin folder on your server. That could definitely cause some conflicts in routing on your site. A proper way to do this would be to host your api project on a separate server and use a sub-domain, such as http://api.my.site/. That way you ensure that your C# site (http://my.site/)would have access to the api (http://api.my.site/). And your dlls, including routing rules, would remain separate.
I'm creating an asp.net MVC3 demo application. I have attached the application with this thread. I'm facing a HTTP Error 403.14 - Forbidden issue when trying to run the application. The issue occurs only in a specific scenario. i.e. in the RegisterRoutes method in the global.asax
has only the below code then the issue occurs.
routes.MapRoute(
"Process1", // Route name
"Process/List" // URL with parameters
);
If we have the above code with default route the application works fine without any issues. The issue occurs when i have this particular code alone in the register routes method.
I tried the solutions given in this link but it did not work.
I assume the issue with that code. If yes then please let me know what causes the issue. I use windows 8.1 & the iis is 8.5.
The code can be found here
You have removed the default route and defined the following route:
routes.MapRoute(
"Process1", // Route name
"Process/List" // URL with parameters
);
In this declaration it is not clear which controller and which action should be
executed. You should explicitly state them:
routes.MapRoute(
"Process1", // Route name
"Process/List", // URL with parameters
new { controller = "MyController", action = "MyAction" }
);
and then make sure that you are making the request to the proper url that you have defined:
http://localhost:1619/Process/List
because you don't have any route configured that is capable of handling the http://localhost:1619/ request that you seem to have made in your screenshot.
Of course having such route is completely constraining. The only controller action you could ever have in your MVC application is the one defined as such in the route config. This is fine in a SPA application where you have a single entry point serving the SPA and a separate ASP.NET Web API but in a real ASP.NET MVC application with multiple controllers and actions it is not going to work. So you probably want to keep the default route in this case and do not experiment in this area.
You must have a default root in your global.asax file to make it work. add one default and then you can add this route.
Assuming that I have the following ASP.NET website:
http://example.com
I want to expose this url:
http://yourCompany.example.com/yourName
and I want it to route to
{myController}/{myAction}/{yourCompany}&{YourName}
Can I setup a route for this in my RegisterRoutes in RouteConfig class?
You will have to redirect from the http://yourCompany.example.com/[0] to http://example.com/yourCompany/[0] as the subdomains are treated as stand-alone domains.
In my MVC project, There is a Default.html file in root folder and route this file as a default route.
routes.MapRoute(
name: "",
url: "Default.html"
//,
//defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It's working fine when I access it like http://localhost:51353/Default.html
I host (include this static file) this project in my web server. But it's showing error:
Is there any additional configuration needed to do this ?
If you want to host a static HTML page within an ASP.net MVC project then you need to configure your routing configuration in MVC to ignore requests for those pages.
It worked locally because you may have set it as start page in Visual Studio. For this to work you need to tell MVC to ignore the route if its for the HTML page or ASPX page. Find your routing configuration section, it’s in RouteConfig.cs under the App_Start folder. Use the IgnoreRoute() method to tell Routing to ignore the specific paths.
routes.IgnoreRoute("Default.html"); //ignore the specific HTML page
Now MVC ignores a request to load the Default.html page and leaves IIS to handle the resource.
As per the MVC Routing you cannot map static files to the routing table, because when MVC Routing Machanism provides direct access to physically exist static files.
In my MVC and WebAPI application I see two different ways of doing routes.
One for MVC which calls RegisterRoutes and passes RouteTable.Routes
One for Web API which calls CustomizeConfig and passes GlobalConfiguration.Configuration.
For WebAPI:
WebApiConfig.CustomizeConfig(GlobalConfiguration.Configuration);
public static void Register(System.Web.Http.HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: ApiControllerOnly,
routeTemplate: "api/{controller}");
}
For MVC:
RouteConfig.RegisterRoutes(RouteTable.Routes);
public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.MapRoute("DefaultRedirect",
"",
new { controller = "Home", action = "Index" });
}
Can someone explain if there is any difference in me registering my routes in one or the other method calls? Also why is it done this way with one using.
MVC routes register with ASP.NET (system.web) route collection. Web API however is designed to run either in IIS on top of system.web or as a self host without changing the code.
Hence Web API has a different registration mechanism, where it can use the system.web routing under the hood, or it's own routing system when using self hosting (Either WCF self host, or Owin host are supported out of the box).
There is one other small difference, Web API routes require naming of the route, where MVC routes do not.
One of the significant differences in the web API compared to traditional ASP.NET MVC controllers is how the web API will route request into the action methods.
With the web API, the HTTP method being used plays a role. The HTTP method is the verb used in the HTTP message and the common verbs are get, post, put, and delete. Those are the verbs that the web API will route by default. You an also handle additional verbs if you need to do something like webDAV. You can do that and handle additional verbs by using an except verbs attribute.
What the web API will do is if there is a request for "/movies", the web API will look for a movie controller and then look for a method on that controller starting with the word "Get." So I could have an action called get movies and because this is an HTTP get message, the framework will invoke get movies. But you could also call it GET whatever is just because it starts with the letters G-E-T that's why that particular action method will receive the request.
The web API also registers these routes slightly differently since there is no action that's going to be in the URL for the routing engine to pick apart, it's using the verb instead. If you look at the default route configuration for a web API, it's done with an extension method MapHttpRoute.
For Web API it is:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
For MVC, it is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Notice that for Web API there is no action in the URL template and it's also important to note that the path of this needs to start with "api".
After looking at this, the proper way to reach a controller (say Movies) is to use /api/movies as a URL on the browser. That will invoke the get method since we have a get request.
Note: Web API controllers inherit from System.Web.Http.Controller, but MVC controllers inherit from System.Web.Mvc.Controller. Both the libraries are different but acts in similar fashion. Web API's MapHttpRoute is defined as an extension method in System.Web.Http class. MVC's MapRoute is defined in System.Web.Mvc as an extension method.