How do I route a webforms page in ASP.NET MVC? - c#

I have an MVC page that has a webforms page that it needs to render:
The virtual directory for the webforms page is:
http://mysite/Report/1
File saved:
~/Areas/Accounts/Views/Invoices/Report.aspx?id=1
How do I map this?
I have mapped it to controller:
return Redirect("~/Areas/Accounts/Views/Invoices/Report.aspx?id=1?id=" + id);
But I get an error.

You want to use the MapPageRoute() method to send something to a specific page:
routes.MapPageRoute(
"ReportRoute",
"Report/{id}",
"~/Areas/Accounts/Views/Invoices/Report.aspx?id={id}"
);

From the way you put it you might not be clear on what you're doing.
Add a Controller (from visual studio) in this folder : ~/Areas/Accounts/Controllers/Report
You would probably have a method display(int id) in your ReportController class. Then by default your URl will look like:
http://mysite/Report/display/1
To customize it you add this into Global.asax.cs :
routes.MapRoute(
"NewRoute", // Route name
"report/{id}", // URL with parameters
new { controller = "report", action = "display", // Parameter defaults
id = UrlParameter.Optional }
);

Related

Why aren't my links taking me into my controller?

I guess I don't completely understand how urls work with C# projects, in the sense that I don't know how to specify a url to go through the controller and not just return a aspx page.
Say I am trying to get to my project's Index page through a Controller named "ScholarshipController.cs". I would think to hit the Index method/action in this controller, my url would be as follows (my app's name is "TuitionAssistance" fyi):
http://localhost/TuitionAssistance/Scholarship/Index
However, running this url just returns the aspx page named "Index.aspx" located in the "Scholarship" view file without hitting the Controller. Why is this happening, and how do I get it to go through the controller so the Index page, when loaded, will have the appropriate information loaded onto it?
Sorry if this is a dumb question. Any insight would be appreciated. Thanks!
Route.config:
using System.Web.Mvc;
using System.Web.Routing;
namespace ScholarshipTuitionAssistance
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/* Scholarship */
/* Scholarship */
//routes.MapRoute("TuitionAssistance",
// "tuition/{name}",
// new { controller = "TuitionAssistance", action = "Index", name = "" });
routes.MapRoute(
name: "TuitionAssistance",
url: "{controller}/{action}/{employee_number}",
defaults: new { controller = "Home", action = "TuitionAssistance", employee_number = UrlParameter.Optional }
);
routes.MapRoute(
name: "Scholarship",
url: "{controller}/{action}/{employee_number}",
defaults: new { controller = "Home", action = "Scholarship", employee_number = UrlParameter.Optional }
);
routes.MapRoute(
name: "Details",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Scholarship", action = "Details", id = UrlParameter.Optional }
);
}
}
}
Your route (URL) cannot match anything that actually exists on the filesystem. In your example here, you apparently have a file, [document root]\Scholarship\Index.aspx. As a result, a request for Scholarship/Index will return that file, instead of invoking the ASP.NET MVC machinery to load a controller action.
In MVC ASP.NET, think of those types of links as a way to call your methods in your controller. When that link is accessed, your controller does a bunch of junk and then returns an ActionResult (or other things). This ActionResult, for the sake of this explanation, is the markup that is written in the corresponding view file. Controller - >index() will return the view called index under views - > controller. If you want to pass information to your view, you will pass a model that has all of your information in it to the view from your index controller (return View(MyFancyModel)). The view will have a razor line at the top such as: #model The.Namespace.Wherever.my.model.is
The scaffolded controllers and views in Visual Studio for the index page specifically, only pass a list of the items in the corresponding database.

asp.net MVC generic action handler

I am making a documentation for the OSS I am about to publish. The url is going to be like /Documentation/{class name} . All the documentation view are named as {namespace}_{classname}. Basically i wonder if there is a way to direct all requests to /Documentation/* to a method inside my Documentation controller so that i can do something like
return View({class name});
instead of having to make a method for each class
You can handle that in routing.
routes.MapRoute(
"Docs", // Route name
"Documentation/{className}", // URL with parameters
new { controller = "Documentation", action = "Show", className = "" } // Parameter defaults
);
That assumes of course that you have a DocumentationController with a Show attribute.

How to 'type' my application, persisting this type through pages

I'm developing an application for a company that sell some produts and this application is responsable for manage products changes and returns. There are different rules for changes and returns but the "screens/views" are all the same. This application must have a different URLs for each type. for example:
www.company.com/change
www.company.com/return
the application need to have a Login page too.
When I access www.company.com/CHANGE the user is redirected to login page and in this page have a label with change text.
When I access www.company.com/RETURN the user is redirected to login page and in this page have a label with return text.
The question is: How to persist this type through pages, reminding that if the user is inside the authentication area of application and logout, He has to return to the right login page, with correct label text.
I tried to store the type in Session, but if the session over, it's impossible to know how parameter i have to pass to Login page (Change or Return)
I tried too, create a new route in Global.asax like to persist the type, like that:
routes.MapRoute(
"qwert", // Route name
"{type}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Login", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
But this url for example: "xxx/home/list" match and i would like that just
return/abc/abc
and
change/abc/abc
to match.
I will persist a record in database with this type, in the end of the process.
How can I solve this situation?
Question needs a bit of clarification but if I understand correctly, simply treats all links and URLs containing the {type} parameter.
I did a small test and what I got was:
In global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"qwert", // Route name
"{type}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Login", action = "Index", type = "", id = UrlParameter.Optional } // Parameter defaults
);
}
And in LoginController you can get the type like:
public ActionResult Index(string type)
{
return View();
}
You can create a custom attribute for the session's expiration like described Here and return the user to the appropriate page www.company.com/{type}

ASP.Net MVC3/4 multiple templates

There is any way to change the defualt views path in MVC3/4. i.e: The Url http://localhost:000/Home (the controller Home) will represent the view at Views/Style1/Home/Action.
Thanks ahead!
Ok, now that I understand the question better after the edit, I think this is what you're looking for:
You can change the ViewLocation in Application_Start().
The example below assumes use of the Razor View Engine.
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine { ViewLocationFormats = new string[] { "~/Views/Style1/{1}/{0}.cshtml" } } );
Answer was partially derived and referenced from this post
You should be able to set the Default route for your application to use a different base path. You can typically set the route in the Global.asax in the RegisterRoutes method.
Example:
routes.MapRoute(
"Default", // Route name
"Style1/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

How to redirect when route is missing "parts" in asp.net MVC?

Let's say I have a url like this:
www.site.com/en/home/list/2010-09-30
The "en" is for English, and I want to make sure there is always a language token set in the url. If a user enters www.site.com/home/list/2010-09-30 I want it to redirect them to www.site.com/en/home/list/2010-09-30 (where en is the default language).
How is this best accomplished in asp.net MVC (version if that matters)?
This would probably best be handled in a custom HttpModule. I'm sure you already know, but just in case, you can default the route value "en" in your route declarations as well. It won't redirect the page but the culture will always be set in code if it's not present in the URL.
context.MapRoute(
"Default",
"{culture}/{controller}/{action}/{id}",
new { culture = "en", action = "Index" });
A simple solution would be to have a wildcard route (This should be at the bottom of your global.asax file):
routes.MapRoute(
"WildCard",
"{*url}",
new { controller = "Main", action = "EnglishDefault" }
);
Then your action would be:
public ActionResult EnglishDefault (string url)
{
return Redirect("/en/" + url);
}

Categories

Resources