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.
Related
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.
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.
I want to do url mapping using coding in asp.net c#
if /default.aspx page is requested then on client site it should be shown /default.
asp.net(c#) 4.0 .net framework
From Scott Guthrie Article
Mapping URLs using ASP.NET Web Forms
ASP.NET 4.0 now allows you to also use the URL Routing engine to map URLs to ASP.NET Web Forms pages as well as ASP.NET MVC Controllers.
Below is an example of how you can use the new MapPageRoute() helper method in ASP.NET 4.0 to map the /default URL to a default.aspx page that lives immediately under the application root directory:
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"default-page",
"default",
"~default.aspx"
);
}
void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
You may like to check this Url Rewriting with ASP.NET and Managed Fusion URL Rewriter and Reverse Proxy. Also check this example:- An Example of URL Rewriting With ASP.NET
Also check out the Microsoft.AspNet.FriendlyUrls to easily use routing and get friendly urls
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.