ASP.NET MVC5 RoutePrefix controller name - asp.net-mvc-5

I'm starting with .NET MVC framework. I'm using 4.5 with MVC5.
I've a Controller "APropos" with a View in a folder "APropos" etc. It works great!
But I want that my URL looks "site.com/A-Propos" and not "site.com/APropos".
I've read some posts about "RoutePrefix" in Controller :
[RoutePrefix("a-propos")]
public class AProposController : Controller
{
public ActionResult Index()
{
return View();
}
}
I also renamed the view folder to "a-propos" but it doesn't work... (ressource is not found)
I'm sure that someone knows the solution!
Thanks for help :-)

Thanks for your help!
It works for actions but for Controller, it doesn't work.
I specify a RouteArea like msdn :
[RouteArea("APropos", AreaPrefix = "A-Propos")]
public class AProposController : Controller
{
public ActionResult Index()
{
return View();
}
}
The url is always "monSite.com/apropos" and not "monSite.com/a-propos"...
I have to do something else ?
Thanks!
EDIT :
I found a solution !
On the Route attribute, I specified the default action name like that :
[RoutePrefix("A-Propos")]
[Route("{action=index}")]
public class AProposController : Controller
{
public ActionResult Index()
{
return View();
}
}
Thank you for your help.
Best regards

Related

Kentico 13 .NET Core MVC Routing Patters

I have the following code in my Startup.cs
endpoints.MapControllerRoute(
name: "Default",
pattern: "{controller}/{action}/{id?}"
);
Routing doesn't work for the following method in the controller and returns 404
public async Task<ActionResult> Index(int id)
{
}
The url I'm trying to navigate is
/home/our-villages/property/100
However, it's working fine without the parameter value '100'. It hits the controller action in this case.
/home/our-villages/property
I believe I'm missing something in reagards to setting up the routing with parameters here. Any idea?
Have you tried making the id parameter for the method optional?
Try doing this
public async Task<ActionResult> Index(int id = 0)
{
}
or
public async Task<ActionResult> Index(int? id)
{
}

SwaggerRequestExample attribute does not work in ASP.NET MVC 5 (.NET Framework 4.5.2)

I am toying with Swashbuckle.Examples package (3.10.0) in an ASP.NET MVC project. However, I cannot make request examples appear within the UI.
Configuration (SwaggerConfig.cs)
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c => {
c.SingleApiVersion("v1", "TestApp.Web");
c.IncludeXmlComments(string.Format(#"{0}\bin\TestApp.Web.xml", System.AppDomain.CurrentDomain.BaseDirectory));
c.OperationFilter<ExamplesOperationFilter>();
c.OperationFilter<DescriptionOperationFilter>();
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
})
.EnableSwaggerUi(c => { });
}
Request example classes
public class EchoRequestExample : IExamplesProvider
{
public object GetExamples()
{
return new EchoInput { Value = 7 } ;
}
}
public class EchoInput
{
public int Value { get; set; }
}
Action
[HttpGet]
[Route("Echo")]
[CustomApiAuthorize]
[SwaggerRequestExample(typeof(EchoInput), typeof(EchoRequestExample))]
[ResponseType(typeof(EchoServiceModel))]
public HttpResponseMessage Echo([FromUri] EchoInput model)
{
var ret = new EchoServiceModel
{
Username = RequestContext.Principal.Identity.Name,
Value = value
};
return Request.CreateResponse(HttpStatusCode.OK, ret);
}
Swagger UI shows xml comments and output metadata (model and an example containing default values), but shows no request example. I attached to process and EchoRequestExample.GetExamples is not hit.
Question: How to make SwaggerRequestExample attribute work in ASP.NET MVC 5?
Note: Windows identity is used for authorization.
I received an answer from library owner here:
Swagger request examples can only set on [HttpPost] actions
It is not clear if this is a design choice or just a limitation, as I find [HttpGet] examples also relevant.
I know the feeling, lot's of overhead just for an example, I struggle with this for a while, so I created my own fork of swashbuckle, and after unsuccessful attempts to merge my ideas I ended up detaching and renaming my project and pushed to nuget, here it is: Swagger-Net
An example like that will be:
[SwaggerExample("id", "123456")]
public IHttpActionResult GetById(int id)
{
Here the full code for that: Swagger_Test/Controllers/IHttpActionResultController.cs#L26
Wondering how that looks like on the Swagger-UI, here it is:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=IHttpActionResult#/IHttpActionResult/IHttpActionResult_GetById

What is analog of Response.AddHeader("Refresh", "10") in ASP. NET MVC5

Would someone tell me if there is an analog of Response.AddHeader("Refresh", "10") in ASP. NET MVC5, please?
I have tried [OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 10)] but it does not work.
[OutputCache] is for, well, caching the output of an action. The Duration param merely tells it how long to cache that output. Neither has anything to do with setting HTTP headers, and certainly will not make a page refresh automatically.
Reponse.AddHeader is still valid in MVC5; you just need to ensure that you have not started the response yet. Unless you're doing something off-the-wall, that's not difficult. If you're returning a ViewResult, for example, just call this first:
Response.AddHeader("Refresh", "10");
return View();
If you're directly writing to the response, then just ensure you add the header before you start doing that.
You can use it directly in your controller
public ActionResult MyAction()
{
Response.AddHeader("Refresh", "10");
return View();
}
Or you can make a custom action filter
public class RefreshAttribute : ActionFilterAttribute, IActionFilter
{
public string Duration { get; set; }
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var duration = 10;
Int32.TryParse(this.Duration, out duration);
filterContext.HttpContext.Response.AddHeader("Refresh", duration.ToString());
}
}
Usage
[Refresh(Duration = "10")]
public ActionResult MyAction()
{
return View();
}

PartialView is not found

The controller is in /Areas/ShoppingCart/Controllers/
I've tried to let the controller return the PartialView which in /Views/Shared folder.
The Code as follows:
namespace MVCIIITourism.Areas.shoppingcart.Controllers
{
public class CartController : Controller
{
public ActionResult GetCart()
{
return PartialView("../../Shared/Views_Cartpatail.cshtml");
}
public ActionResult AddToCart(int id)
{
var currentCart = Operation.GetCurrentCart();
currentCart.AddProduct(id);
return PartialView("../../Shared/Views_Cartpatail.cshtml");
}
}
}
But the chrome shows the error message that is
Not Found PartialView in /Areas/ShoppingCart/Controllers/
How should I fix the problem?
If your partial is in the shared folder you don`t have to pass the entire path
Shared folder it`s access by all controllers
Simple use:
return PartialView("Views_Cartpatail.cshtml");

How to attach an existing View to a controller action?

How can I attach an existing View to an Action?
I mean, I've already attached this very View to an Action, but what I want is to attach to a second Action.
Example:
I've an Action named Index and a View, same name, attached to it, right click, add view..., but now, how to attach to a second one? Suppose an Action called Index2, how to achieve this?
Here's the code:
//this Action has Index View attached
public ActionResult Index(int? EntryId)
{
Entry entry = Entry.GetNext(EntryId);
return View(entry);
}
//I want this view Attached to the Index view...
[HttpPost]
public ActionResult Rewind(Entry entry)//...so the model will not be null
{
//Code here
return View(entry);
}
I googled it and cant find an proper answer...
It's possible?
you cannot "attach" actions to views but you can define what view you want be returned by an action method by using Controller.View Method
public ActionResult MyView() {
return View(); //this will return MyView.cshtml
}
public ActionResult TestJsonContent() {
return View("anotherView");
}
http://msdn.microsoft.com/en-us/library/dd460331%28v=vs.98%29.aspx
Does this help? You can use the overload of View to specify a different view:
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
ViewBag.Message = "Hello I'm Mr. Index";
return View();
}
//
// GET: /Test/Index2
public ActionResult Index2()
{
ViewBag.Message = "Hello I'm not Mr. Index, but I get that a lot";
return View("Index");
}
}
Here is the View (Index.cshtml):
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>#ViewBag.Message</p>

Resources