I have an MVC controller with a single method which contains a few arguments, the last of which is a filename with an extension and it is this last argument which causes issues.
Say the format I wish to create is:
http://example.com/worker/90bef68f718a434bb588120e717fa29c/foo.txt
This URL will 404, however if I remove the period or add a trailing / then the handler is hit and executed normally.
Unfortunately the URL format I am trying to implement cannot accommodate either of these work arounds.
The route is defined as follows:
routes.MapRoute(
name: "Worker Handler",
url: "worker/{guid}/{fileName}",
defaults: new { controller = "Worker", action = "Index" }
);
Some reading suggested changing the url template to include {fileName} to {*fileName}, however that too did nothing to fix this.
I can suggest two solution.
First Use route like this ( FileName and FileExtention in separate argument)
routes.MapRoute(
name: "Worker Handler",
url: "worker/{guid}/{fileName}/{fileextention}",
defaults: new { controller = "Worker", action = "Index" }
);
Another solution is to add following thing in web.config.
<system.webServer>
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
</system.webServer>
Get Help from http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html
Route should be like this.
routes.MapRoute(
name: "Worker Handler",
url: "worker/{guid}/{fileName}",
defaults: new { controller = "Worker", action = "Index" }
);
Hope this help you.
Related
I want to change the site I'm working on so that when someone creates an article, the path would be "Articles/ViewArticle/2016-10-04/test" instead right now it's just "Articles/ViewArticle/test". When I implemented it, I got an instant 404.
I tried creating this route before & after the Default Route, but still no go:
routes.MapRoute(
name: "ArticlesDefault",
url: "{controller}/{action}/{date}/{id}",
defaults: new { controller = "Articles", action = "ViewArticle", date = UrlParameter.Optional, id = UrlParameter.Optional }
);
I should note that the "id" is actually stored in the database as [date]/ArticleTitle.
Would anyone be able to help?
Use attribute routing instead. By using attribute routing you can construct the routes using the values of parameters passed to action methods.
More information on attribute routing is here https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
if you do not want to use attribute routing, you can use constraints parameter of MapRoute method
Edit:
You can try any one of following approach
Without using attribute routing. I assume that your Id is in the form of {date}/articleTitle
routes.MapRoute(
name: "ArticlesDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Articles", action = "ViewArticle" },
constraints: new { id = “\\d+” });
Using attribute routing. Best approach will be to separate article date and article title into two distinct parameters. Then you can use the following code.
[Route("Articles/ViewArticle/{articleDate:datetime}/{articleTitle}")]
public ActionResult ViewArticle(string articleTitle, DateTime articleDate)
{
//your action code goes here
}
I want to get URL of the default controller's default action method link, such that if I change default controller and action in route config then should be updated in the view too.
e.g.
Url.Action(defaultAction,DefaultController);
//output should be like
Url.Action("Index","Home")
Assuming the default route is named Default (in your RoutesConfig):
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
You can use Url.RouteUrl to get the url:
Url.RouteUrl("Default")
See MSDN
i am searching the net non-stop.
Can pls someonw point me in the rigth direction?
My Problem:
Normally mvc display: {Controller}/{Action}
and it gives us => Home/Index
What i want:
I want different URL without changing action or controller name like =>
MyPage/MyIndex
Should i be looking into the routing or is there an other way.
try this:
routes.MapRoute(
name: "CustomRoute",
url: "MyPage/MyAbout",
defaults: new { controller = "Home", action = "About"}
);
I'm working on a multilingual Asp.Net MVC project.
Default language is Turkish and alternate language is English.
I need to make urls seo friendly. For example some urls for web site are like;
Home/About:
www.example.com/hakkinda
www.example.com/about
Home/Contact:
www.example.com/iletisim
www.example.com/contact
Home/Faq :
www.example.com/sikca-sorulan-sorular
www.example.com/frequently-asked-questions
There are some pages like this. I also have Turkish and English seo friendly urls for the pages.
I register routes:
routes.MapRoute(
name: "hakkinda",
url: "hakkinda",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "about",
url: "about",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "iletisim",
url: "iletisim",
defaults: new { controller = "Home", action = "Contact"}
);
routes.MapRoute(
name: "contact",
url: "contact",
defaults: new { controller = "Home", action = "Contact"}
);
routes.MapRoute(
name: "sss",
url: "sikca-sorulan-sorular",
defaults: new { controller = "Home", action = "Faq"}
);
routes.MapRoute(
name: "faq",
url: "frequently-asked-questions",
defaults: new { controller = "Home", action = "Faq"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
I also have a Home/ChangeLanguage method, which sets thread's culture by given culture name (tr/en)
User can change language by clicking a button
My first question is i need to change url automatically when user changes language.
Initial url : www.example.com/hakkinda
After user changes language to English url should redirect to www.example.com/about
Again if user changes language to back to Turkish it should be redirected to www.example.com/hakkinda
This has to be applied to all pages.
My second question is for Seo optimization, i need to add below tags to all pages in order to say bots alternate pages by language
<link rel="alternate" hreflang="tr" href="http://www.example.com/hakkinda"/>
<link rel="alternate" hreflang="en" href="http://www.example.com/about"/>
I need to add these link tags to Site Layout and it must be matched with the current url.
Could you please give advice about how i can achieve these ?
You could create an ActionFilter that will given each request check the culture sent to the server (done via cookie?) and then have it on every request look up the controller and action against separate dictionaries, try to match the oposite localisation on either the controller or Action and if it hits a match (i.e. your currently navigating to /about but your in Turkish, it would match /about as being english and then perform a redirect to /hakkinda
I am new to MVC and my question is this how can I setup my sites root to point to a specific Controller + Action and then in the Web.config file set the location + path of the root of the site, so eg: http://localhost:8080/ to be able to be accessed by all anonymous and logged in.
I have been playing with the location and path but just cant figure it out and in my Global.asax, I am not sure I have the right root to Home + Index as controller + Action.
Here is some code:
web.config (snippets)
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880"/>
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
I am not sure what the <location path=""> of the root of the site should be.
Global.asax (snippets)
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 }
);
}
The default route in Global.asax for the site I would like it to go to the Home Controller and the Action Index, so when you type http://localhost:8080/
Thanks in advance.
You are already doing it in the third parameter of MapRoute.
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
These specify your default controler and action, exactly as you want it.