I have a controller "Home" and 2 actions inside it called "DeliveryReturn" and one called "BasketReturn" like this:
public ActionResult DeliveryReturn(string id)
{
return View();
}
public ActionResult BasketReturn(string id)
{
return View();
}
".../Home/Return/Delivery/10" -> DeliveryReturn should be called.
".../Home/Return/Basket/10" -> BasketReturn should be called.
I tried setting up my route somewhat like this. I am really lost though and it seems like it doesn't make sense at all.
routes.MapRoute(
name: "Return",
url: "Home/Return/{type}/{id}",
);
Update:
I figured it out, that I could do something like this. However, this would require me to register two routings. How would I do it with only one?
routes.MapRoute(
name: "DeliveryReturn",
url: "Home/Return/Delivery/{id}",
defaults: new { controller = "Home", action = "DeliveryReturn", id= ""}
);
routes.MapRoute(
name: "BasketReturn",
url: "Home/Return/Basket/{id}",
defaults: new { controller = "Home", action = "BasketReturn", id= ""}
);
Related
i am tring to have links link this
/View/test
it is working only if it is int like /View/1
my view action link
#Html.ActionLink(item.className, "Viewe", "subject", new {id =item.className },null)
my control action
public ViewResult Viewe(string id)
{
//some database
return View();
}
i get this error Server Error in '/' Application.
if i change the id into anything it works fine but the link will looks like
this : View?anything=par
image of the error
route code
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
this is working for me
#Html.ActionLink(item.ClassName, "Viewe", "Subject", new { Id = item.ClassName }, null)
I have an issue and cant seem to resolve it. I have a url, thats like:
myurl/?culture=fr
what I want is
myurl/fr
My Controller looks like :
public ActionResult Index(string culture = null)
and my routeConfig:
routes.MapRoute(
name: "Languages",
url: "{controller}/{action}/{culture}"
);
This results in the page isn't redirecting properly.
Any hints to solve it?
Give default Controller and action in your route if you are appending "fr" to the root of the URL(www.yourUrl.com/fr). like this :-
routes.MapRoute(
name: "Languages",
url: "{controller}/{action}/{culture}",
defaults: new { controller = "Home", action = "Index"}
);
Replace "Home" with your default controller and "Index" with default action.
I inherited some ASP.Net MVC code and am tasked with adding some new features. I am a complete beginner using ASP.Net MVC and come from a background of mainly using Web Forms.
I added a new controller (ApiController) and I added the following actions to it:
// GET: /Api/Index
public string Index()
{
return "API Methods";
}
// GET: /Api/DetectionActivity
public JsonResult DetectionActivity()
{
var detections = from d in db.Detections
orderby DbFunctions.TruncateTime(d.CreationTime)
group d by DbFunctions.TruncateTime(d.CreationTime) into g
select new { date = g.Key, count = g.Count() };
ViewBag.DetectionCounts = detections.ToList();
return Json(detections, JsonRequestBehavior.AllowGet);
}
My RouteConfig.cs has the following registered routes.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
This looks like the tutorials I've been reading but it's not working and I'm probably missing something.
If I go to localhost:21574/api I see the output from the Index() action, "API Methods".
If I go to localhost:21574/api/DetectionActivity it throws a 404 with the following data in the response:
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:21574/Api/DetectionActivity'.",
"MessageDetail":"No type was found that matches the controller named 'DetectionActivity'."
}
I'm thinking there is something I need to do that I'm not.
Any suggestions on what to do next?
Update 1
I tried this with my RouteConfig.cs
routes.MapRoute(name: "ApiController",
url: "{controller}/{action}"
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
These were my results:
If I go to localhost:21574/api I see the output from the Index() action, "API Methods". Same as before.
If I go to localhost:21574/api/DetectionActivity it throws a 404 with the following data in the response:
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:21574/Api/DetectionActivity'.",
"MessageDetail":"No type was found that matches the controller named 'DetectionActivity'."
}
Same as before.
If I go to localhost:21574/Api/Api/DetectionActivity it throws a 404 with this data in the response:
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:21574/Api/Api/DetectionActivity'.",
"MessageDetail":"No type was found that matches the controller named 'Api'."
}
Now it's saying it can't find a controller named "Api".
from your Route Config
the URL should be: localhost:21574/Api/Dashboard/DetectionActivity
or if you really need localhost:21574/Api/DetectionActivity (not recommended)
change your Register method in WebApiConfig class to
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = RouteParameter.Optional }
);
I'm having a problem with MVC 4, and I guess it's something really trivial, but it's been bugging me for the last day and I can't seem to figure it out.
I have this url:
http://www.example.com/my-dashed-url
I have a Controller named:
public class MyDashedUrlController: Controller
{
}
I have only two Routes like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("my-dashed-url",
"my-dashed-url/{action}",
new { controller = "MyDashedUrl", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
I get to the index just fine. However, when I do this:
public ActionResult Index()
{
if (NoUserIsLoggedOn)
return RedirectToAction("Logon", "MyDashedUrl");
return View();
}
public ActionResult Logon()
{
Contact c = GetContact();
return View(c);
}
It doesn't redirect me to the "Logon" action properly.
It should redirect me to:
http://www.example.com/my-dashed-url/logon
but instead it tries to redirect me to:
http://www.example.com/logon
... which doesn't work (404 Not Found)
I'm missing something. Can anyone spot it? If anyone needs any more information, let me know.
And it's EVERY RedirectToAction that does the same thing in this controller. A Html.BeginForm("Logon", "MyDashedUrl") would also generate:
http://www.example.com/logon
I guess it has to do something with the Routes I defined, but I can't find the faulty one, seeing as they're all the same. If I disable all of my Routes besides the default one from MVC, the problem remains the same
Make sure that you have declared this custom route BEFORE the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"my-dashed-url",
"my-dashed-url/{action}",
new { controller = "MyDashedUrl", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Remember that routes are evaluated in the order you declared them. So the first route that matches a request will be used. If you declare your custom route after the default one, it is the default route that will match the request.
I am a beginner in MVC 4. I have an url ../Home/NameDetailsPage/20 where controller= Home, action=NameDetailsPage and pageIndex= 20. How do i write my route engine for this url?
routes.MapRoute(
......
.....
);
In controller, NameDetailsPage works pretty fine for default page such as int page=2 :-
public ActionResult NameDetailsPage(int? page)
{
var context = new BlogContext();
IQueryable<string> list;
list = from m in context.Blogs.OrderBy(m => m.BlogId)
select m.Name;
ViewBag.total = list.ToArray().Length;
ViewBag.page = page;
var pageNumber = page ?? 1;
ViewBag.page1 = pageNumber;
return View("NameDetails", list.Skip(pageNumber * 4).Take(4));
}
But the pageNumber is always 1 whatever pageIndex in the url. So It shows same result for all the pageIndex. How can I set pageNumber other than 1. Thanks in Advance.
When you have a route like the following
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
The defaults object properties (controller, action, id) are what can be passed to the action (if the action has parameters that match the name).
So if you have an action:
public ActionResult NameDetailsPage(int? page)
there are no values being passed from the URL as page.
You can either change {id} to {page}, or preferably:
public ActionResult NameDetailsPage(int? id)
Based on your comment you are most of the way there. You just need to define the parameter you are passing to the action. In this case switching {id} for {page} and altering the defaults to suit.
routes.MapRoute(
name: "Page",
url: "Home/NameDetailsPage/{page}",
defaults: new { controller = "Home", action = "NameDetailsPage", page = UrlParameter.Optional } );