“routes.MapRoute("static",
"welcome",
new { controller = "Home", action = "index" });”
So does it mean go to Home/Index? What is "welcome" in the URL?
First is the name of the route, second is the url and the third part is the default values.
Please refer to the official documentation next time, for instance this tutorial:
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs
This specific route would apply only for url: {root}/welcome , where {root} may be something like www.mysite.com and it will use controller with name Home and activate its action Index.
Related
I have the following in views/page2/index.cshtml:
#Html.ActionLink("Continue...", "Home");
I'd like the above to link to the root Home controller's view. What I get now is localhost/page2/home/ instead of localhost/.
Any ideas how to use the correct path?
In that case you do not need to use the html helper to generate the link. Use pure html.
Home
This link will link to the root of your web application.
The prototype for the ActionLink looks like this:
public Microsoft.AspNetCore.Html.IHtmlContent ActionLink (
string linkText,
string actionName,
string controllerName,
string protocol,
string hostname,
string fragment,
object routeValues,
object htmlAttributes);
So when you call #Html.ActionLink("Continue...", "Home"); you are passing in the linkText and the actionName. Since this is all the router has to work with, it assumes the actionName you want (Home) is within the same Controller as the current page.
In order to tell the router that you want to switch to another Controller, you need to pass that Controller name in as well.
#Html.ActionLink("Link Text", "Action", "Controller");
I'm going to assume that when you're passing in Home you actually wanted to reach the Index action from the Home controller. If so, this would look like this:
#Html.ActionLink("Continue...", "Index", "Home");
I have two controllers. One is login.js and the another one is home.js. I want to redirect to home controller from login controller with a value. To redirect to home controller, i wrote,
response.redirect('/home');
But, i cannot pass value here. When rendering a view with a value,
data = {'value':'hello'};
response.render('view_login',data);
I want to do the same thing. I can pass value while rendering to the view but not when changing controller. Can i do this?
response.redirect('/home',{data: data});
you can use query parameters for this.
When you are redirecting your home page or any other controller then you can write like this
res.redirect(/<some controller>?data=<some data>)
and in that controller where you want to use this value..you can write like this
function login(req,res){
var data = req.query.data;
}
I have come across what appears to be an inconsistency in MVC 5 regarding the Html.Actionlink. In different cshtml files I can use the same code, but the url target that is generated is different.
For example, this line of code:
<td>#Html.ActionLink(item.Description, "Edit", new { item.ParentTableID }) </td>
generates this URL
localhost\MyControllerClass\Edit?ParentTableID=35
That then properly calls the ActionView method Edit and feeds the parameter with 35 as expected.
However, in another cshtml file, this line
<td>#Html.ActionLink("Edit", "EditChild", new { id = f.ApplicationTableFieldID})</td>
produces this url
localhost/MyControllerClass/Edit/7
and when it hits the EditChild Action View, the parameter is null.
I have seen this now a couple of times and not yet been able to understand what makes the difference. But I need the first result.
Thanks.
Ensure that your ID parameters are named correctly in both your Action method and your ActionLink Html helper. The visual difference comes from MVC default routing and how it can take a parameter named ID and put it in the URL without the query string (? followed by stuff)
If your action method looks like this
public ActionResult EditChild(int ParentTableID){}
Then you will need to have your ID parameter named ParentTableID when you pass it back in your URL
<td>#Html.ActionLink("Edit", "EditChild", new { ParentTableID = f.ApplicationTableFieldID})</td>
Should now produce the following URL
localhost\MyControllerClass\EditChild?ParentTableID=3
I have been looking through many forums but I was unsuccessful :(
I need to display the content of a OpenCart custom page in the search page.
Ta very much
You just need to execute that custom controller, save its output in the data list of the search page template and display it there where ever you want
(1) Open the file "<OC_ROOT>/catalog/controller/product/search.php", you will find a class named "ControllerProductSearch", we are interested in the function "index()"
(2) Find the statement
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
(3) Add the route of your custom controller in the array $this->children, now OC will automatically evaluate your custom controller and send the HTML in a variable to the template, the variable name will be the last component in the route (e.g if your route is common/my_customer_controller, the name will be $my_custome_controller)
(4) Open the search page template file "<OC_ROOT>/catalog/view/theme/<YOUR_THEME_FOLDER>/template/product/search.tpl", and use the variable there
P.S. Make sure that you define your custom controller in the same way other controllers are defined (file naming style, class name based on the route ...), other wise the above solution won't work
The MVC project that I am currently working on uses Regions so that we can localise pages etc.
I have spotted a problem with our Error page. We have turned the custom error pages on in the web.config file. If we are on a page lets say : /IT/News/Index and we get an error, when it redirects it will go to /Error and there will be no routevalue attached to it.
Is there away to ensure that the langauge routevalue is retained by the Error page?
I have searched around and cannot find a solution at the moment and was wondering if anyone else could help or point me in the right direction?
Hope that this all makes sense. Any help is much appreciated.
If you're getting physically redirected to /Error then it's not because of the MVC HandleErrorAttribute. It's probably due to your Web.Config having system.web/customErrors defined for error handling. Using the HandleErrorAttribute causes it to inject a specific view instead of the view you would have normally returned but does not redirect you to a different action by default. The problem is when redirected because of customErrors, there is no inherant information available to tell you where they came from. But using HandleErrorAttribute DOES cause some info to be populated for you. Specifically it creates a HandleErrorInfo to use as a view model and passes that to the view you specify. For example, here's one that is reigstered in the /App_Start/FilterConfig.cs file.
public class FilterConfig
{
public static void RegisterFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute {View = "Error"});
}
}
When you redirect to an error View using the HandleErrorAttribute, certain information is populated for you. The HandleErrorInfo view model will contain the ControllerName of the original controller requested, and the ActionName of the original action. Also, the ViewData and the TempData from the original request will be copied into the ViewData and Temp data for the request to the Error view. With that information it should have what you need. Be aware that not all errors happen inside of an Action however, and exceptions that don't happen in an action will not be caught by the HandleErrorAttribute. So you'll still need to use something like customErrors (or system.webServer/httpErrors if you're doing it inside of IIS7+) to handle exceptions that occur elsewhere in your app.
Here's a link to the HandleErrorAttribute file on CodePlex in case you're wondering what it does. HandleErrorAttribute.cs
I'm not sure if this solution meets you requirements. You can override in your base controller OnException and then redirect to a specific page.
protected override void OnException(ExceptionContext filterContext)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString();
//get other stuff from routing
//here you can do redirect or other stuff
//if handled exception
//filterContext.ExceptionHandled = true;
base.OnException(filterContext);
}
It depends how you're getting to the error pages, really. If you're using an ActionFilter-based method to catch exceptions, then you can get route values from the context that gets passed into the OnException method. If you're using a redirect from a catch block, then you can push the relevant information into TempData or pass it directly as a parameter, depending on how you're doing that redirect.
You can add a custom HandleErrorAttribute or use a base controller to be inherited by all your controllers. Either way, you need to get the RouteData object, like this
var routeData = filterContext.RouteData;
with that object, you can get all the route values accordingly to your needs. Check the object definition in MSDN site for more detail
Say you have the following route
routes.MapRoute(
"Language", // Route name
"{language}/{controller}/{action}/{id}", // URL with parameters
new { language = "en", controller = "Sites", action = "Index", id = UrlParameter.Optional } // Parameter default
Then routeData.Values.Keys will tell you the name of the parameter and routeData.Values.Values the value itself
Then, wherever you handle the exception, you can store the route data in a TempData variable, like this
TempData["RouteData"]
And after that, it will be available on your error page
#model System.Web.Mvc.HandleErrorInfo
#{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
#TempData["RouteData"];