I am trying to create a new route... Here the code:
routes.MapRoute(
name: "Services",
url: "Administration/{controller}/{action}/{id}",
defaults: new { controller = "Services", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Now when I click on an actionlik, it redirects me on the correct controller:
#Html.ActionLink("Services", "Index","Services")
Here the Index Action in the Services controller
public ActionResult Index()
{
return View(); //Here it is where I stop debug
}
I arrive in the action.. and now my custom route should redirect to my view. Correct?
I let you see what I see when i stop debug:
How you can see everything seems is well valorized. But when I obtain following error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Services/Index.aspx
~/Views/Services/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Services/Index.cshtml
~/Views/Services/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
It does not look the view inside Administration folder!!
In this way it works instead:
return View("~/Views/Administration/Services/Index.cshtml");
Where is wrong?
Thank you
Looks like you don't quite understand routing, or that routing is not the same thing as view resolution. Changing the URL to access an action method doesn't make it search new folders for your views. So you need to do one of the following:
Leave the view in one of the folders the error message suggests.
Manually specify the view: return View("~/Views/Administration/Services/Index.cshtml");
Learn how to use MVC areas.
Manually add in your additional folders to the list the view engine uses.
Implement your own view engine.
Personally I recommend option 1.
Related
i am working on a web project in asp.net mvc.
This is code in my RouteConfig
This is url
localhost:55960/Home/myAction/80102/Aus-won-the-match-by-9-wickets
but i want to make it like
localhost:55960/myAction/80102/Aus-won-the-match-by-9-wickets
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{Title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, Title = "" }
);
I have applied many suggestions available on this platform but not worked.
Is there any one who can explain in detail
So you want your controller to be at the root, like domain.com/?id=1 or domain.com/list?id=1
instead of domain.com/controller/list?id=1 ?
You can add to your config
config.MapHttpAttributeRoutes();
Then for that controller,
[RoutePrefix("")]
public class HomeController : Controller
But this may get confusing to keep track of if you have many controllers.
And it may be better to configure routes.MapRoute() to point to a default controller
I want to be able to rewrite my URL's to a single level URL. This means that i need to make a dynamic(will change in each language) rewrite rule, like this:
Original Url: http://www.mydomain.com/account/pages/13
I always want to show the URL in a single level:
http://www.mydomain.com/my-page-title
never no more than one slash / after the domain name.
I also need to be able to translate the page title in the above example:
http://www.mydomain.com/my-translated-page
How do I achieve this, and it should be able to change this on runtime - that is to "improve" the url, just like rewrite rules in htaccess
You have to create your own Routes. As you are using the MVC4 put this custom route above the Default Route of the Application.
routes.MapRoute(
name: "Custom_Route",
url: "My-Page-Title/{id}",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
And for translating a Url in Mvc
You can create you own custom routes like this,
This should be the first route
routes.MapRoute(
name: "Default_Custom",
url: "MyCustom-{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and make sure that you have this line in Global.asax
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
This woks for the all request like #Html.ActionLink("Home", "Index", "Home")
But the request like
#Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })
Will be handled by the default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
That's not a simple and easy to answer question. Complete description can't be placed here. But I try to guide you with some suggestions and Links may be useful. Considering that with The default form of MVC Routing is /controller/action/id, we'll ensure uniqueness of each part during programming and help of database's unique Ids, In this way, no need to pay attention on uniqueness of each part And routing is infallible always.
By the way, First of all, ensure you'll generate non-repetitive URLs to be caused ambiguity in Routing. So each Page title should be unique!
After that you need to have lower cased and dashed page titles. Or write following code to put the title to lower case and replace non-alphanumeric characters with dashes to employ in routing implementation.
public static string SeoName(string name)
{
return Regex.Replace(name.ToLower().Replace(#"'", String.Empty), #"[^\w]+", "-");
}
Finally you need to have Translation provider Interface to use in Routing. See Translating routes Article to implement this feature too.
Note that you must order routes according to their priority as desired to perform in order subsequently.
I'm new to the whole routing thing in MVC so I'm not sure what to make the title of my question. Basically, we have Google Analytics that pics up stats based on the current url. We added in a referrer to the route so that we can give a link to our partner sites such as:
http://www.mysite.co.za/PartnerSite/home/index
When the user comes to our site directly, ie: http://www.mysite.co.za, I want the url to show "Website" as the referrer.
So in essence,
http://www.mysite.co.za
must change to
http://www.mysite.co.za/Website/Home/Index
My route is current configured as:
routes.MapRoute(
name: "Search",
url: "{referer}/{controller}/{action}",
defaults: new { referer = "Website", controller = "Home", action = "Index" }
);
Can this be done in the routes or should I get my system administrator to update IIS settings and redirect?
Thanks,
So to close this off I needed to have a solution posted.
The bottom line is that this is something for IIS, and it does not seem possible using Routes.
ok,suppose when the user hits the url http://www.mysite.co.za then suppose it accessing Home Controller's Index View Action.Because /Home/Index is the First Default Page in MVC so its not the Part of URL.
So for your Case You should write the code like this-
routes.MapRoute(
name: "Website",
url: "/Website/Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
Please Try it and let me know the result.
I am planning to write custom ASP.NET MVC4 routing for the problem described below.
By default [http://localhost:1603] URL should show login page after user logged in then URL should contain username name in the URL like [http://localhost:1603/username] and other action methods should access like [http://localhost:1603/username/profile].
Please guide me how to proceed with this problem.
Thanks in advance.
By default [http://localhost:1603] URL should show login page
To do this you can change Default Url Route's default values.Like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
You should change default Controller and Default Action name.For example I change them like: Account and Login.
URL should contain username name in the URL like [http://localhost:1603/username]
Now you need a custom route,maybe something like:
routes.MapRoute(
name: "Custom",
url: "{username}/{action}",
defaults: new { controller = "UserController", action = "Index" }
);
With this route (I assume you have (UserController and an Index action in it):
http://localhost:1603/username
Will go to the Index action of UserController.
http://localhost:1603/username/profile
Will look for a Profile Action inside of UserController.Ofcourse you should get username from RouteData dictionary inside of your Action.Otherwise that seems pointless.
I'm a fan of MvcCodeRouting (there's a NuGet package for it). Take a look at the doco for base routes. It will do what you're trying to achieve.
I am Inside Index View of Home controller.
I have created one controller called EstimationTracker with default Index action method. I have also created Index view of EstimationTracker. But, nothing is there inside Index action method of EstimationTracker controller.
Now, I want to create Html.ActionLink inside Index view of HomeController to navigate to Index view of EstimationTracker controller.
I have written the following code to achieve this:
#Html.ActionLink("Estimation Tracker", "Index", "EstimationTracker", null, new { })
In the Browser, if I click on "Estimation Tracker" hyperlink, the page is generating the following URL:
http://localhost:8416/EstimationTracker/
and popping up the following error:
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
When I append Index to the above URL i.e.:
http://localhost:8416/EstimationTracker/Index
Now, the page is fine.
How can I make the page run smoothly without any error.
Update:
I'm using Default Routing only. like below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}