MVC4 Dynamic Controller Path? - c#

I have a SiteController class in my MVC4 project,the 'Site' url is working fine, but I need a dynamic url part right after 'Site', I need the url to look like this:
mysite.com/Site/{DYNAMICSTRING}/Users(or whatever)
{DYNAMICSTRING} can be the name of a subsite, so the controller should check if that subsite does actually exist in the database.
Right now I'm using Query strings, but that is not what my client wants.
How can I do that?
Additional details
My routing:
routes.MapRoute(
"Subdomain", // Route name
"{controller}/{action}/{dynamicString}", // URL with parameters
new { controller = "Site", action = "Subdomain" } // Parameter defaults
);
My controller:
public ActionResult Subdomain(string dynamicString)
{
return View();
}
the value of dynamicString is null when I navigate to: /Site/Subdomain/SomeString

You have to configure routing. For example if you have Homecontroller:
public class HomeController:Controller
{
public ActionResult Subdomain(string dynamicString)
{
return View();
}
}
then you have to configure your routing something like that
routes.MapRoute(
"Subdomain", // Route name
"{controller}/{action}/{dynamicString}/anyOtherParams", // URL with parameters
new { controller = "Home", action = "Subdomain", dynamicString = "" } // Parameter defaults
);

You can do it like this:
routes.MapRoute(
name: "Default", // Route name
url:"Site/{dynamicstring}", // URL with parameters
defaults: new {controller = "Site", action = "Index" } // Defaults
);
you can keep adding parts to the url part like so
url:"Site/{dynamicstring}/{anythingelse}" // url:"Site/{dynamicstring}/{anythingelse}/{bla}/Test/{hello}/etc..."
or you can also have a catch all route like this:"
routes.MapRoute(
name: "Default", // Route name
url:"{*all}", // catch all
defaults: new {controller = "Site", action = "Index" } // Defaults
);
and fetch all other parts in your controllers index action, by splitting them on /
Make sure you put the custom route before your default otherwise the default route will pick it up.
in your controller you get something like this:
public ActionResult Index(string dynamicstring, string anythingelse)
{
return View();
}
and if you then pass in a url like this:
http://www.mysite.com/Site/test.nl/jordy
your dynamicstring will have the value "test.nl" and your anythingelse will have "jordy"
I hope this helps

Related

UrlHelper in Controller Action does not build correct URL

I have an issue, when I'm trying to build the url for action in controller, the value of vm after assigning is "/". If I try to create url with other action name then everything works fine, like Url.Action("Edit", "Contact").
public class ContactController : Controller
{
public ActionResult List()
{
string vm = Url.Action("Create", "Contact"); // equals "/"
string editUrl = Url.Action("Edit", "Contact"); // all is fine
return View("List", vm);
}
public ActionResult Create()
{
return HttpNotFound();
}
public ActionResult Edit()
{
return HttpNotFound();
}
}
What's wrong with that code?
It is because your route specifies them as defaults.
Your route is:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Contact", action = "Create", id = String.Empty }, null);
Essentially, it is because you specify the default values controller = "Contact", action = "Create". When you specify these as default you are saying if the value is not provided in the URL then use these.
For examples all these URLs are the same: /, /Contact & /Contact/Create. By default MVC generates you the shortest URL.
You could either change the default values or remove them like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { id = String.Empty }, null);

Why isn't string parameter being routed to an action?

In my MVC5 application I am trying to pass a string to an action.
In PodcastsController I have an action called Tagged:
public ActionResult Tagged(string tag)
{
return View();
}
For example, if I wanted pass the string Testto the Tagged action, it would look like this in the url:
/Podcasts/Tagged/Test
I have a route set up like this:
routes.MapRoute(
"Podcasts",
"Podcasts/Tagged/{tag}",
new { controller = "Podcasts", action = "Tagged", tag = UrlParameter.Optional }
);
Edit I am calling the Tagged action like this:
if (!string.IsNullOrEmpty(tagSlug))
{
return RedirectToAction("Tagged", "Podcasts", new { tag = tagSlug });
}
When I set a break point on the Taggedaction, tag is always null
Can anyone see what I am doing wrong?
I'm pretty sure there's something wrong with the route but I can't figure out what...
The reason you are getting a null is because you are not actually passing it a parameter called tag in your code:
if (!string.IsNullOrEmpty(tagSlug))
{
return RedirectToAction("Tagged", "Podcasts", new { tagSlug });
}
When you omit the property name it takes the variable name, so you are actually passing a variable tagSlug which your action does not accept. Try this instead:
if (!string.IsNullOrEmpty(tagSlug))
{
return RedirectToAction("Tagged", "Podcasts", new { tag = tagSlug });
}
It must be working exacly like this....
Then go to http://localhost:64147/podcast/tagged/hi and recive hi as a value of tag
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MyRoute",
url: "podcast/tagged/{tag}",
defaults: new { controller = "Podcasts", action = "Tagged", tag = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I'm sorry i'm asking here, but did you check that your route precedes any other routes that could overlay?
The routing is done by taking them in order. The first route that corresponds to the given parameters is chosen so if you have this route first
{controller}/{action}/{id}
and then
Podcasts/Tagged/{tag}
you will get null because the first route is chosen
Route order actually matters

How to Create a dynamic Controller to handle many static Views in ASP.NET MVC?

I have a bunch of mostly-static pages (about 40),
like: order-form01.html, order-form02.html, orderform03.html etc..
Should each of them have its own Controller/Action, or is that possible to have one dynamic Controller/Action for all of them?
My Url should look like this: http://MyProject/GlobalController/IndividualView and for the above example: http://MyProject/OrderForm/order-form01, http://MyProject/OrderForm/order-form02 etc..
Thanks in advance.
Yes it's very easy AND you don't need a switch statement or any other redundant logic.
public class MyController
{
public ActionResult Page(string file)
{
return View(file);
}
}
The magic is in the Route Map:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// New MapRoute for your 40+ files..
routes.MapRoute(
"OrdeForm",
"OrderForm/{file}",
new { controller = "MyController", action = "Page", {file} = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
Additionally:
I pass the View name in the query string.
Is not required, but is supported. The following urls will work:
// Url Parameters
http://MyProject/OrderForm/order-form01
http://MyProject/OrderForm/order-form02
// Querystring Parameters
http://MyProject/OrderForm?file=order-form01
http://MyProject/OrderForm?file=order-form02
The only catch is that you need to rename your html files to cshtml and place them in the correct directory for the ViewEngine to find.
#Erik, I also bit of new to mvc . Could you please explain your route map as of how is it possible with default raute again and again
Routes are broken down into 3 values:
Controller
Action
Parameter(s)
At a bare minimum, the controller and action are required. Where the values come from is not dependent on the Url. For example, in the following Url and Map Route...
// Url
http://MyProject/
// MapRoute
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);
// Controller named "Home" matches the default in the above route
// Method named "Index" matches the default in the above route
public class HomeController {
public ActionResult Index() {
return new EmptyResult();
}
}
... everything still works because we provided a default value for the controller and action.
Ok let's break down the URL you want:
http://MyProject/OrderForm/order-form01
http://MyProject/OrderForm/order-form02
http://MyProject/<identifier>/{parameter}
You have one identifier that tells me route (OrderForm) and one changing value that because it changes and you want one value, should be a parameter.
http://MyProject/<identifier>/{file}
The name of the parameter makes no difference as long as it matches the signature of the controller method:
http://MyProject/{Controller}/{file}
public class HomeController {
public ActionResult Index(string file) {
return new EmptyResult();
}
}
or
http://MyProject/{Controller}/{einstein}
public class HomeController {
public ActionResult Index(string einstein) {
return new EmptyResult();
}
}
I named the parameter file, because it tells me it's the parameter is a name of a file, whereas the name einstein has no inherent description so is a terrible name for a variable.
http://MyProject/{Controller}/{file}
// MapRoute
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);
// Controller named "Home" matches the default in the above route
// Method named "Index" matches the default in the above route
public class HomeController {
public ActionResult Index() {
return new EmptyResult();
}
}
Now we only want this route to run when the identifier is OrderForm so we don't allow that to be a value, we hard code it.
url: "OrderForm/...
Next we have a value that keeps changing, so we to add url parameter:
url: "OrderForm/{file}"
Now we have an issue because we aren't allowing MVC to parse values from the url to populate Controller nor Action so we must supply them.
routes.MapRoute(
name: "",
url: "OrderForm/{file}",
defaults: new { controller = "Home", action = "Index", file = "" }
);
Here we've mapped the url http://MyProject/OrderForm/{file} to:
public class HomeController {
public ActionResult Index(string file) {
return new EmptyResult();
}
}
Now I would choose to to update the defaults to something more specific and descriptive:
routes.MapRoute(
name: "",
url: "OrderForm/{file}",
defaults: new { controller = "OrderForm", action = "Index", file = "" }
);
public class OrderFormController {
public ActionResult Index(string file) {
return new EmptyResult();
}
}
Hope that all makes sense.
After the question edited :my solution is, you can have one controller/action and it should call view (cshtml). Your querystring data should be pass to view as of viewbag variable and partial views should be called acording to the viewbag variable. noo need of editing routing table even(if you are willing to pass it as a query string).
//your routeconfig will be
routes.MapRoute(
name: "default",
url: "{controller}/{file}",
defaults: new { controller = "OrderForm", action = "Index", file = "" }
);
//here no need to have 40 routing table one is enough
//your controller/action will be
public class OrderForm
{
public ActionResult Index(string file)
{
ViewBag.Orderform=file
return View(file);
}
}
//view bag variable is accessible in view as well as in javascript
But I would say as best practice, you can modify default routing to access all urls and navigate it to same controller/action and let that action to return the view. After that use angular / knockout js to handle client side routing and based on it the partial views should be loaded.(still your url will be different for your 40 pages but noo need to pass it as query string)
//your route table will be
routes.MapRoute(
name: "default",
url: "{controller}/{file}",
defaults: new { controller = "OrderForm", action = "Index"}
);
//your controller will be
public class OrderForm
{
public ActionResult Index()
{
return View(file);
}
Navigation should be handled by client side routing

How to route request to www.MyDomain.com to specific controller class and method

In my MVC application, when user goes to www.MyDomain.com/Home, this request is processed in HomeController class Index method due to following routing entry in Global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Let's say if user goes to www.MyDomain.com/SomeParameters, I want this request to be processed in MyController class Index method. An example for the parameters will be www.MyDomain.com/John. For this I have created following entry in Global.asax but it does not seem to get hit. Can anyone point out what I am doing wrong here?
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "MyController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The routing handles John as the controller, not the action. So in your example John is the controller and because you don't provide an action, it takes Index as the default action.
If you want www.MyDomain.com/John to be routed to controller MyController, action Index and a parameter that contains John, a solution could be to add the following route (before the default route):
routes.MapRoute(
name: "MyController",
url: "{myparameter}",
defaults: new { Controller = "MyController", Action = "Index" });
And the controller:
public ActionResult Index(string myparameter)
{
return View("whatever");
}
This will lead www.MyDomain.com/John to the Index action with myparameter = "John".
ps. In the example myparameter is mandatory.
In order to have www.MyDomain.com/SomeParameters you simply need to create a route where the Controller and the Action method are defaulted since they will not be provided in the URL.
Make sure the route definition only includes the someparameters and does not have anything else. This way you can just treat anything in the URL after / as a parameter.
routes.MapRoute(
name: "MyController",
url: "{someparameters}",
defaults: new { Controller = "MyController", Action = "Index" });
public ActionResult Index(string someparameters)
{
...
return View();
}

Razor file name with a dash

I need my page names to have a dash in the name. E.G our-vision
I'm new to MVC & c# so I may be going about all this wrong.
Here is my controller:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
//
// GET: /our-vision/
public ActionResult ourVision()
{
return View();
}
}
And then in my views, I have Views/Home/ourVision.cshtml.
When I compile and go to http://localhost/ourVision it works, but when I go to http://localhost/our-vision it does not.
Here is my routing:
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You'll need to do a few things in order to achieve that.
First, to achieve our-Vision, you'll need to give your action method the ActionName attribute, like so:
[ActionName("our-Vision")]
public ActionResult ourVision()
Next, you'll have to rename your ourVision.cshtml view to be our-Vision.cshtml
Finally, whenever you're using Url.Action or ActionLink, you need to use our-Vision and not vision, like so:
Url.Action("our-Vision", "Home");
IMHO
The best way to do this - is define new route in route engine:
routes.MapRoute(
"OurVision", // Route name
"our-vision", // URL with parameters
new { controller = "Home", action = "ourVision" } // Parameter defaults
);

Categories