Request ["fileName"] return null?? Why?
Full image: http://i.stack.imgur.com/U1MzX.jpg
Controller:
Full image: http://i.stack.imgur.com/DCQNG.jpg
Route
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"AdminLogin",
"Admin/",
new { controller = "Account", action = "Login" }
);
//NossaIgreja/{nomePastor}
routes.MapRoute(
"Pastor", // Route name
"Pastor/{id}", // URL with parameters
new { controller = "NossaIgreja", action = "Pastor" } // Parameter defaults
);
routes.MapRoute(
"Download", // Route name
"Downloads/Boletim/{year}/{week}", // URL with parameters
new { controller = "Downloads", action = "DownloadBoletim" }, // Parameter defaults
new { year = #"\d+", week = #"\d+" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
, new string[] { "SextaIgreja.Web.Controllers" }
);
}
Area registration
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "SextaIgreja.Web.Areas.Admin.Controllers" }
);
}
}
Your controller action should take "fileName" parameter, instead of doing a Request["fileName"]. As long as your query string parameter matches the name of the parameter in your controller mvc should pass it in automatically.
If Remover is an MVC action than I cannot see the parameter in Remover Action called fileName
It means when call Remover action the fileName in your querystring is not getting automatic model binding
Related
Following all suggested remedies, still my method is not getting called.
The method being called is in the same controller. TEST can be called either by itself through a link on a separate page, or when a user selects a saved "TEST" view that they wish to load again. In this case, the below LoadUserSelectedTransaction is called.
I've tried specifying the controller to use with no luck, hard coding requestType, [HTTPPOST] attribute, removing FormMethod.Post from the view
View is as follows:
using (Html.BeginForm("LoadUserSelectedTransaction", "TransactionList", FormMethod.Post))
Controller is as follows
public class TransactionListController : Controller
{
public static bool userLoaded = false;
public static string userFriendlyName;
public static string transaction;
public static string requestType;
//[HttpPost]
public ActionResult LoadUserSelectedTransaction(FormCollection formdata)
{
userLoaded = true;
userFriendlyName = formdata["UserLoadedTransaction"];
var model = new MyTransactionsDBHandle();
transaction = model.ReturnUserLoadedTransaction(userFriendlyName, User.Identity.Name);
var myTransaction = new MyTransactionsModel();
requestType = myTransaction.ReturnRequestType(transaction);
if (requestType == "")
{
requestType = formdata["CurrentTransactionPage"];
}
// need to check for duplicate transactions types, such as CC_SALE also being a POS Surcharge transaction
// CC_SALE has EMV, ship to, bill to etc, POS Surcharge does not
// CC_VOID with and without card - With card will have NGT_TRACKDATA tag
// DB_SALE with and without cashback - Cashback will have NGT_DC_CASH_BACK_AMOUNT tag
if (requestType == "DB_SALE")
{
if (transaction.Contains("NGT_DC_CASH_BACK_AMOUNT"))
return RedirectToAction("DB_Sale_With_Cash_Back");
return RedirectToAction("DB_Sale_No_Cash_Back");
}
else if (requestType == "CC_SALE")
{
if (transaction.Contains("NGT_EMV_DATA"))
return RedirectToAction(requestType);
return RedirectToAction("CC_POS_Surcharge");
}
else if (requestType == "CC_VOID")
{
if (transaction.Contains("NGT_TRACKDATA"))
return RedirectToAction(requestType);
return RedirectToAction("CC_Void_No_Card");
}
else
return RedirectToAction(requestType);
}
}
Method being called:
public ActionResult TEST(FormCollection formdata)
{
ViewModel model = new ViewModel();
if (userLoaded) // static in same controller
{
userLoaded = false;
if (requestType == "My Transactions")
{
var currentTransactionPage = formdata["CurrentTransactionPage"];
return RedirectToAction(currentTransactionPage);
}
model.myTransactions.ParseUserTransactionString(transaction, model);
}
return View(model);
}
Any suggestions are greatly appreciated, thanks in advance!
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ClientTablePages",
url: "{controller}/{action}/{pageNumber}",
defaults: new { controller = "Client", action = "Index", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "DivisionTablePages",
url: "{controller}/{action}/{pageNumber}",
defaults: new { controller = "Division", action = "Index", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "MerchantTablePages",
url: "{controller}/{action}/{pageNumber}",
defaults: new { controller = "Merchant", action = "Index", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "TerminalTablePages",
url: "{controller}/{action}/{pageNumber}",
defaults: new { controller = "Terminal", action = "Index", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "ResetPassword",
url: "{controller}/{action}/{id}",
defaults: new { Controller = "User", action = "UserResetPassword", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MyTransactions",
url: "{controller}/{action}/{pageNumber}",
defaults: new { Controller = "MyTransactions", action = "MyProfile", page = UrlParameter.Optional }
);
}
}
RedirectToAction returns a 302 redirect to the browser which is always a GET request.
In comparison Server.TransferRequest(url, true) performs a server redirect preserving the postBack values using a POST request.
P.S. Browser won't be aware of this redirect hence the browser url will not reflect the new address
In my ASP MVC application, I need to show the URL path like this:
domain.com/viewName
I have the route config in the follow way:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.RouteExistingFiles = false;
routes.IgnoreRoute("{*allfiles}", new { allfiles = #".*\.(css|js|gif|jpg|png)" });
routes.MapRoute(
"Theme",
"theme/{theme}/{page}",
new { controller = "Theme", action = "Index", page = UrlParameter.Optional },
new[] { "Tenant.Controllers" }
);
routes.MapRoute(
"ThemeSlider",
"theme/slider/{id}",
new { controller = "Theme", action = "Slider" },
new[] { "Tenant.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Tenant.Controllers" }
);
routes.MapRoute(
"Default_tenant",
"{page}",
new { controller = "Home", action = "Index", page = UrlParameter.Optional },
new[] { "Tenant.Controllers" }
);
routes.MapMvcAttributeRoutes();
}
But it does not works, I have to specify the page parameter in the follow way: domain.com/?page=viewName. How should be the route config that leaves the follow routes:
domain.com.co/controller/action/
domain.com.co/view
Any sugestion ?? Thanks in advance
You mean to get rid of the controller? This sort of thing will work, but beware of conflicts between routes in different controllers:
routes.MapRoute(
name: "HomeRoute",
url: "{action}",
defaults: new {controller = "Home", action = "Index"},
constraints: new { homeControllerConstraint = new HomeControllerConstraint() }
);
Now every action in the home controller maps to Domain.com/Action instead of Domain.com/Home/Action.
Here is some reflective code to efficiently work out whether a route is in the home controller, for instance.
internal class HomeControllerConstraint : IRouteConstraint
{
private static readonly Lazy<HashSet<string>> homeMethods = new Lazy<HashSet<string>>(CreateHomeMethods);
private static HashSet<string> CreateHomeMethods()
{
return typeof(HomeController).GetMethods().Select(e => e.Name.ToLower()).ToSet();
}
public static bool Exists(string name)
{
return homeMethods.Value.Contains(name.Trim().ToLower());
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return homeMethods.Value.Contains(values["action"].ToString().ToLower());
}
}
I have a problem with routing in ASP.NET MVC 5. I create two Areas "Public" and "Admin" and I set Public/NewsController/Index default site in url localhost/Example.WebUI but now I can't use localhost/Example.WebUI/Admin...
My code with T4MVC:
AdminAreaRegistration.cs
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { area = MVC.Admin, controller = MVC.Admin.Hierarchy.Name, action = MVC.Admin.Hierarchy.ActionNames.Index, id = UrlParameter.Optional }
);
}
}
PublicAreaRegistration.cs
public class PublicAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Public";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Public_default",
"{controller}/{action}/{id}",
new { controller = MVC.Public.News.Name, action = MVC.Public.News.ActionNames.Index, id = UrlParameter.Optional }
);
}
}
RegistrationRoutes.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = MVC.Public.Name, controller = MVC.Public.News.Name, action = MVC.Public.News.ActionNames.Index, id = UrlParameter.Optional },
namespaces: new[] { "Example.WebIU.Areas.Public.Controllers" }
);
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = MVC.Admin.Hierarchy.Name, action = MVC.Admin.Hierarchy.ActionNames.Index, id = UrlParameter.Optional },
namespaces: new[] { "Example.WebIU.Areas.Admin.Controllers" }
);
}
And Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AutoMapper.Mapper.Initialize(cfg => cfg.AddProfile<AutomapperProfile>());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
Can you help me?
The issue is that all of your route URLs are exactly the same except for the Admin area. MVC will always use the first matching route and ignore every route that follows, so it is important to both configure the routes in the right order, and to ensure that the route doesn't match any URLs except for the ones it should. This problem and possible solutions for it are best described in Why map special routes first before common routes in asp.net mvc?.
The simplest fix is to use a literal segment in the URL to ensure it only matches a specific set of URLs.
PublicAreaRegistration.cs
public class PublicAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Public";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Public_default",
"Public/{action}/{id}",
new { controller = MVC.Public.News.Name, action = MVC.Public.News.ActionNames.Index, id = UrlParameter.Optional }
);
}
}
RegisterRoutes.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "MyRoute",
url: "MyRoute/{action}/{id}",
defaults: new { controller = MVC.Admin.Hierarchy.Name, action = MVC.Admin.Hierarchy.ActionNames.Index, id = UrlParameter.Optional },
namespaces: new[] { "Example.WebIU.Areas.Admin.Controllers" }
).DataTokens["area"] = "Admin";
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = MVC.Public.Name, controller = MVC.Public.News.Name, action = MVC.Public.News.ActionNames.Index, id = UrlParameter.Optional },
namespaces: new[] { "Example.WebIU.Areas.Public.Controllers" }
);
}
Note you also have MyRoute registered in the wrong order. This route must be placed before Default in order for it to have any effect at all. As previously mentioned, you also need a way to ensure that not all 3 segment URLs will match. The above shows the use of a literal segment to do this, but you could do more advanced matching with a Regex route constraint or a Custom route constraint.
Finally, you are missing the .DataTokens["area"] = "Admin" needed to set the route to a specific area (this is needed if you don't define the route inside of the AdminAreaRegistration class).
I am trying to remove area name and controller name from URL.I am able to remove area name. But if trying to remove controller name then "Page not found error" displayed. Below is the code snippet which I have used to remove the area and controller name.
public class HomeAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Home";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
//context.MapRoute(
// "Home_default",
// "Home/{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional }
//);
//context.MapRoute(
// "Home_default",
// "{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional },
// new { controller = "(Home)" }
//);
context.MapRoute(
"Home_default",
"{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "(Home)" }
);
}
}
In the registerarea function the first two routes(commented) are working perfectly. if I use the first one the URL comes with Area/Controller/Action. If I use second one the area is not coming in URL.The URL come ups with Controller/Action.
In the third one I am trying to remove both area and Controller
Is any thing wrong in my third route. Please suggest
Have you tried this:
routes.MapRoute("default", "{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional});
I need do put a new home in new area but im have a error:
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
my new area
Areas/Administrativo/Controllers/HomeController
Areas/Administrativo/Views/Home
my AdministrativoAreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Administrativo_default",
"Administrativo/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
in Global i have
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Preparacao.Gerenciar.Web.Controllers" } // Parameter defaults
);
}
You should specify the namespace constraint in your area route registration (check if the namespace is correct):
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Administrativo_default",
"Administrativo/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Preparacao.Gerenciar.Web.Areas.Administrativo.Controllers" }
);
}
the same way you did with your main route registrations:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "Preparacao.Gerenciar.Web.Controllers" }
);
}