MVC area under new URL - c#

I have two projects what were developed separated and now i need to merge the second one with the first.
The first one is just a website and the second is English test that will have his on 'inside' URL's. What i need is the test to run in a URL like:
mywebsite.com/english-test/begin
mywebsite.com/english-test/questionary
The thing is, my website project's routes works on a different way, i have a database that has my URL's and i check it to see if it's a valid URL. This is the route that will call my class:
routes.MapRoute(
"Default",
"{*RedirectUrl}"
).RouteHandler = new RouteHandler();
This is my RouteHandler class (i'm gonna put it on a pastebin because the code is kind of big): http://pastebin.com/nFfciyBe.
And this is my class that researches the DB for the URL: http://pastebin.com/sCEEKL9b
The English test will have his 'authorized' URL's on this same database, it will work just as the website.

Managed to get it working after using MVC Areas and making a few changes on my RouteHandler class making it check for areas on the DB.

Related

Host WebApi and C# site under the same domain

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.

.Net MVC 3 403.14 error

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.

MVC RouteConfig

I've set up an MVC project, it works perfectly. But I upgraded with a new controller, added some new views, nothing special... The web page works almost perfect when online except that it can't find my new views online.
I really don't know why, because it works in all debug modes in localhost on my machine, but not when published, I did publish the entire solution.
I've also noticed that I can't add new pages to my working home-controller either, I can only edit the ones that I have.
I checked RouteConfig and there are only the default basic settings
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I've created a new controller called Questionnaires, do I have to add this to RouteConfig to make it run online?
Browser address:
www.<address>.com/home/OneView
But with the new Views I want this for the new pages:
www.<address>.com/Questionnaires/Mixer
When browsing to the address I receive this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Questionnaires/Mixers
You said it can't find your views, but routing isn't concerned with views. It's concerned with mapping requests to a controller and action method. The action method is what returns the view. If you created a new controller called QuestionnairesController, then you should be able to access it at /Questionnaires. And since that URL doesn't specify an action method, it will by default (because of your configured route defaults) look for an action method named Index.
If you want to access the Mixer action method in the QuestionnairesControllers at /Questionnaires/Mixer, that should work fine based on the routing you showed us.
When you do return View(); in your Mixer action method, it will by default look for a view at ~/Views/Questionnaires/Mixer.csthml (among several other locations, but that's where you most commonly put it).
This is a bit embarrassing, but I did not notice that i published my project in Debug mode, set to Release solved the problems. Never missed this before.
Sorry about all this, have mercy...

MVC RouteConfig vs URL rewrite (in Global.asax)

Myself and a colleague had a problem that we solved in two different ways. But we don't know which is best.
We have a generic MVC page that is populated with specific data (widgets, content, etc) from a database. The user enters a specific URL (user friendly, so trying to keep the query string disguised if we can help it).
Now, the generic page has to take this URL and use it to get the corresponding data from the database to generate a specific page.
Solution 1: In the Global.asax file rewrite the URL, basically creating a Querystring that the generic controller can understand.
Soloution 2: Use the RouteConfig.cs file to force all page requests to route to the generic controller, which then reads the URL.
Any ideas,
Thanks.
RouteConfig.cs is the cleanest way.
If you change your routing configuration, then by using Url.RouteUrl or Url.Action your generated Url's will update along with your routing configuration.

Registering routes on session start not application start

I am trying to register the route collection in .net based on each session. The code I have works fine, you goto website/username and it loads the correct sub pages, but you have to restart the application to goto website/username2 to load those sub pages. But when the application is restarted the second one works, but then not the first one.
Is there some way to create a different route collection per session not per application using system.web.routing.
Routes are linked to route names and are stored globally for the web application, so you can't really define routes per session.
Can you give an example of why you need different routes for different users?
Most likely it can be solved by simply using route patterns, like setting RouteUrl to somehting like : "member/logo/{size}/{UserName}.jpg", where you can specify UserName and size when generating the route url via Page.GetRouteUrl()
You can create your own route handler. Take a look at : ASP.Net MVC Framework - Create your own IRouteHandler.
Using such approach, you will be able to route differently per request. Each request can then take a look at your session values to get the correct handler.
RouteTable.Routes is static so the same routes are shared across all Sessions. So you can't have a different set of routes for each session.

Categories

Resources